Monday 7 December 2020

Fibonacci Series using For Loop

import java.util.*;
public class FibonacciSeries 
{
	public static void main(String[] args) 
	{
		int a=0,b=1,c;
		Scanner s = new Scanner(System.in);
		System.out.println("\n Enter Limit...");
		int size = s.nextInt();
		System.out.print("\n"+ a +" "+ b);
		for(int i=0; i<size; i++)
		{
			c=a+b;
			a=b;
			b=c;
			System.out.print(" " + c);
		}		
	}
}

Output:

Enter Limit...
10

0 1 1 2 3 5 8 13 21 34 55




No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32