Skip to main content

Write a Program in Java to Print Fibonacci Series | ICSE

ICSE Program
/*
    A Program which prints Fibonacci Series (0 1 1 2 3 5 8...)
*/

   import java.util.Scanner;
   class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
  int n;
long first, second, third;
first = 0; second = 1; third = 0; //initializing the variables
                                                                                                     
            System.out.println("Enter the limit of the Series : "); //asking for the Series Limit
n = sc.nextInt(); //storing the value in "n"
System.out.println("---Fibonacci Series---\n");
System.out.print(first+"\t"+second); //printing the first two no. (0,1)
  for(int i = 2; i<n; i++) {
third = first + second; //calculating the 3rd term (0,1,"1")
System.out.print("\t"+third+"\t"); //printing the 3rd number, i,e. "1"
first = second; //exchanging the values of 1st & 2nd.
second = third;
}
}
   }
Output :
Output of the Code on BlueJ

Comments