Sunday, August 8, 2010

finding Fibonacci series in Java

import java.util.*;
/**
 *
 * @author Akhter Wahab
 */

public class Main {

    /**
     * @param args the command line arguments
     */
    public static long fib(int n) {
        if (n <= 1) return n;
        else return fib(n-1) + fib(n-2);
    }

    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int N = Integer.parseInt(scan.nextLine());//taking input from the user the limit of the Fibonacci series
        for (int i = 1; i <= N; i++)
            System.out.println(i + ": " + fib(i));
    }

}

4 comments:

  1. Recursion is used in this fibonacci finding class

    ReplyDelete
  2. Or maybe pass n in on the command line :/

    ReplyDelete
  3. yes definitely its a good suggestion thanks Anonymous...

    ReplyDelete
  4. now N can be taken form command line..4 finding Fibonacci series..

    ReplyDelete