C Program to Generate Fibonacci Series Using Recursive Function

Question: Write a program in C language to generate Fibonacci series up to n terms recursively, where n is given by user.

Generating Fibonacci Series Recursively in C


#include<stdio.h>
#include<conio.h>

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

int main()
{
 int t, r;
 clrscr();
 printf("How many terms?: ");
 scanf("%d", &t);
 for(i=1;i<=t;i++)
 {
  r = fib(i); /* Normal Function Call */
  printf(“%d\t”, r);
 }
 
 getch();
 return(0);
}

int fib(int n) /* Function Definition */
{
 if(n == 1)
 {
  return(0);
 }
 else if(n == 2)
 {
  return(1);
 }
 else
 {
  return( fib(n-1) + fib(n-2) ); /* Recursive call as fib() calls itself */
 }
}

Output

How many terms? 6↲
0     1     1     2     3     5