Java Program for Fibonacci series

Java Program to find Fibonacci series :

/*Write a program to find Fibonacci series of a given no.
  Example :

  Input - 7
 Output - 1 1 2 3 5 8 13 2


class Fibonacci{

      public static void main(String args[]){

          int num = Integer.parseInt(args[0]);                        //taking no. as command line argument.

          System.out.println("Fibonacci Series");

          int f1, f2=0, f3=1;

          for(int i=1;i<=num;i++){

             System.out.print(" "+f3+" ");

             f1 = f2;

             f2 = f3;

             f3 = f1 + f2;




Output Of Java Program for finding Fibonacci Series

 Logic to generate Fibonacci series Program in java?

 A Fibonacci series is a mathematical series which starts with 0 and 1, and then it keeps on adding the last to numbers to generate the fibonacci series, like 0 1 1 2 3 5 8 13 etc, as 0+1 =1, the 1+1 = 2, 1+2=3, 3+2=5, and so on for the next upcoming elements of the Fibonacci series.

 In the above Fibonacci series program, we have pass the command line arguements at run time itself as a number after writing the command "java Fibonacci no. as arguement" as shown in the above output image of Fibonacci series program. In case you dont pass the arguements at command line, it will give an error message stating that "Exception in thread main java.lang.ArrayOutofBoundxception at Fibonacci.main ". So dont forget to pass command line arguement at the time of executing this Fibonacci series program of Java in your Cmd.



Post a Comment