Fibonacci Series:
The Fibonacci series is the special series of the numbers where the next number is obtained by adding the two previous terms. The terms of the Fibonacci series are 0,1,1,2,3,5,8,13,21,34….
The first term is 0 and the second term is 1.
The next term is obtained as 0+1=1.
Similarly, the next term after 1 is obtained as 1+1=2.
Therefore, the next term after 34 is:34+21=55
and the next term after 55 is: 55+34=89
PsedoCode:
Let n be the term of the Fibonacci series.
fibonacci(int n)
{
if(n==1)
{
return(0);
}
else if(n==2)
{
return(1);
}
else
{
return(fibonacci(n-1)+fibonacci(n-2));
}
}
Code in C:
#include<stdio.h>
#include<conio.h>
int fibonacci(int); /*recursive function definition*/
int main()
{
int n; /* to ask which term of Fiboancci series*/
int fib; /* The fib term that is to be printed */
printf("Enter the number n to know the Fibonacci term: ");
scanf("%d",&n);
fib= fibonacci(n); //recusive function call
printf("\nThe %dth term of the Fibonacci series is: %d",n,fib);
getch();
return(0);
}
int fibonacci(int x) /* Recusive function*/
{
if(x==1)
{
return(0);
}
else if(x==2)
{
return(1);
}
else
{
return(fibonacci(x-1)+fibonacci(x-2));
}
}
Output:
Also Read: Recursive Program to find the product of two natural numbers
Please Comment down below if you are confused in any step.