Tuesday 10 May 2011



Program to find Fibonacci series of a given no


Code:
/*Write a program to find Fibonacci series of a given no.
  Example :
        Input - 8
        Output - 1 1 2 3 5 8 13 21
*/

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;
          }
   }
}

Comment below for your Query and Feedback... :)

Related Posts :



0 comments:

Confused? Feel free to ask

Post a Comment