C Program to Display Fibonacci Series Using Recursion

In this program, we will display Fibonacci Series upto the user provided terms using Recursion. Recursion is the process of defining a problem n terms of itself. A recursive function is a function that calls itself during its execution. The process may repeat several times, outputting the result and the end of each iteration. The function fact() in program uses recursion to find factorial of a number. The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. Here the next number is the sum of previous two numbers.



Source Code
#include<stdio.h>
#include<conio.h>
long int fib(long int n);
void main()
{
    long int res, n;
    int i;
    printf("Enter the number of terms: ");
    scanf("%ld", &n);
    for(i=0; i<n; i++)
    {
        res = fib(i);
        printf("%ld, ",res);
    }
    getch();
}
long int fib(long int n)
{
    if(n==0 || n==1)
        return (n);
    else
        return fib(n-1) + fib(n-2);
}
Output
Enter the number of terms: 12
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,