C Program to Find nth Term of Fibonacci Series Using Recursive Function

In mathematics, Fibonacci sequence is number series, such that each number is the sum of the two preceding numbers, starting from 0 and 1.

First 8 Fibonacci terms are: 0, 1, 1, 2, 3, 5, 8, 13.

Fibonacci terms can be generated recursively as:

         0                  if n=1
fib(n)=  1                  if n=2
         fib(n-1)+fib(n-2)  otherwise

nth Fibonacci Term Using Recursive Function


#include<stdio.h>

/* Function Prototype */
int fib(int );

int main()
{
    int nth, term;

    printf("Which term? ");
    scanf("%d", &nth);

    /* Normal Function Call */
    term = fib(nth);

    printf("%dth term of Fibonacci series is %d", nth, term);

    return 0;
}

int fib(int n)
{
    if(n==1)
    {
        return 0;
    }
    else if(n==2)
    {
        return 1;
    }
    else
    {
        /* Recursive function call */
        return fib(n-1)+fib(n-2);
    }
}

Output

Which term? 8
8th term of Fibonacci series is 13