C program for Fibonacci series using recursion

Logic:

  1. input number of terms
  2. Find the fibonacci series by assigning first two Fibonacci series 0 and 1 
  3. The keep on adding previous two Fibonacci numbers to get third Fibonacci number using recursive function.(Function that calls itself again and again is called recursion)
				
					#include<stdio.h> 
#include<conio.h> 
int f(int); 
void main() 
{
int n, i = 0, c; 
clrscr(); 
printf("Enter number of terms:");
scanf("%d", &n); 
printf("Fibonacci series terms are:\n"); 
for (c = 1; c <= n; c++) 
{ 
printf("%d\n", f(i));
i++;
} 
getch(); 
}
int f(int n) 
{
if (n == 0 || n == 1) 
return n;
else 
return (f(n-1) + f(n-2)); 
}
				
			

Leave a Reply