Wednesday, November 13, 2019

Fibonacci with Recursion

public class Recursion {

    public static void main(String[] args) {
        for (int i = 1; i < 10; i++)
            System.out.println(fib(i));
    }

    public static long fib(long num) {
        if (num == 1) {
            return 0;
        }
        if (num == 2) {
            return 1;
        }
        return fib(num - 1) + fib(num - 2);
    }
}

No comments:

Post a Comment