Monday, 7 December 2020

Fibonacci Series using While Loop

public class fibUsingWhile 
{
	public static void main(String[] args) 
	{
		int a=0,b=1,c,i=0;
		System.out.print("\n"+ a +" "+ b);
		while( i<5 )
		{
			c=a+b;
			a=b;
			b=c;
			System.out.print(" " + c);
			i++;
		}		
	}
}

Output:

0 1 1 2 3 5 8



No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32