Compute the Fibonacci Sequence Using Recursion - C Function

C examples for Function:Recursive Function

Description

Compute the Fibonacci Sequence Using Recursion

Demo Code

#include <stdio.h>

int fib(int);
int main()// w ww  .  ja va  2  s .  co  m
{
    int counter = 24;

    printf("The first %d Fibonacci numbers are:\n", counter);
    for (int k = 0; k < counter; k++)
    {
        printf("\t%d ", fib(k));
        if (((k+1) % 6) == 0) printf("\n");
    }
    return 0;
}

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

Related Tutorials