Web Toolbar by Wibiya

Pages

Tuesday, June 28, 2011

Write a function to generate nth Fibonacci number.

This can be done with both iteration and recursion.

Solution #1: Here is an implementation using recursion,

int fib_rec(int n)
{
    if(n == 0)
 return 0;

    if((n == 1) || (n == 2))
        return 1;
    
    return fib_rec(n-1) + fib_rec(n-2);
}

No comments: