Fibonacci series using Recursion
The series 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ......... is known as Fibonacci series where every third term is obtained by summing the previous two terms. The first two terms of this series are fixed 0 and 1. The general formula for the Fibonacci series is given by the following relation
0 if n=0 or n=1
Fibonacci (num) = 1 if n=2
Fibonacci (num-1) + Fibonacci (num-2) if n>2
Write a program to print the Fibonacci series by using recursion function.
#include<stdio.h>
#include<conio.h>
int fibonacci(int num)
{
if(num==0||num==1)
return(0);
else if(num==2)
return(1);
else
return(fibonacci(num-1)+fibonacci(num-2));
}
void main()
{
int n, i=0;
clrscr();
printf("Enter number for fabonacci series:");
scanf("%d",&n);
printf("The Fibonacci Series:\n");
while(i<n)
{
i++;
printf("%d",fibonacci(i));
}
getch();
}
Output: Enter number for Fibonacci series: 10
The Fibonacci Series: 0 1 1 2 3 5 8 13 21 34