You are here: Home / Topics / Print Fibonacci numbers example using Java

Print Fibonacci numbers example using Java

Filed under: Java on 2023-08-18 06:46:16

//  Program to Calculate the Fibonacci Series of the Given Number.

class  FibonacciTest 
{
public static void main( String args[ ] )
{
 int n, i , a = 0, b = 1, c = 0;
  
 n = 6;

 System.out.print( " " + a + " " + b );   
 for( i=1 ; i<=(n-2) ; i++ )
    {
  c = a + b;
  System.out.print( " " + c );
  a = b;
  b = c;
    }
   }
}


Output:

0  1  1  2  3  5

About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.