Friday, July 13, 2018

Fibonacci Number

Problem statement:
Given a number n, print n-th Fibonacci Number.
Input : n = 2 
Output : 1 
Input : n = 9 
Output : 34
public class FibonacciSeries {
    static int fib(int n) {
        if (n <= 1)
            return n;
        return fib(n - 1) + fib(n - 2);
    }
    public static void main(String args[]) {
        int n = 9;
        System.out.println(fib(n));
    }
}
Output:
34

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...