Question

Write a recursive function that accepts an int argument in n. This function returns the nth Fibonacci number. Call the function in main() to print fibonacci sequences.

Source Code

#include <stdio.h>

int fib(int);

int main()
{
    int x;
    printf("The first 10 Fibonacci numbers are:\n");
    for (x = 0; x < 10; x++)
    {
        printf("%d ", fib(x));
    }
    return 0;
}

int fib(int n)
{
    if (n <= 0)
        return 0;
    else if (n == 1)
        return 1;
    else
        return fib(n - 1) + fib(n - 2);
}

Output

The first 10 Fibonacci numbers are:
0 1 1 2 3 5 8 13 21 34