Saturday, January 14, 2012

1a Recursive Fibonacci


import java.io.*;
 public class Fibonacci
   {
      public static void main(String[] args) throws Exception
        {
System.out.println("Enter n value");
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int n;
n=Integer.parseInt(b.readLine());

          for (int i = 1; i <= n; i++)
           {
             int f = fib(i);
             System.out.println("fib(" + i + ") = " + f);
           }
          System.exit(0);
     
        }

      public static int fib(int n)
        {
            if (n <= 2)
              return 1;
            else
              return fib(n - 1) + fib(n - 2);
        }
   }

No comments:

Post a Comment