Fibonacci series in java - Java @ Desk

Thursday, May 16, 2013

Fibonacci series in java












public class FibonacciSeries {
    public static void main(String args[]) {
        int number = 10;
        int current = 0;
        System.out.println("Fibonacci series for "+number + " : ");
        for (int i = 1; i <= number; i++) {
            current = fibonacci(i);
            System.out.println(" " + current);
        }
    }

    public static int fibonacci(int number) {
        if (number == 1)
            return 1;
        else if (number == 2)
            return 1;
        else {
            return fibonacci(number - 1) + fibonacci(number - 2);
        }
    }
}

Output :

Fibonacci series for 10 :
1
1
2
3
5
8
13
21
34
55






No comments:

Post a Comment