Write a program to calculate nth number of the Fibonacci series.
In this post we shall write a program in c that calculate the nth number of the Fibonacci series.In that program user will enter the position of the element and the program will display the corresponding number of Fibonacci series .
As we know the Fibonacci series is = 1, 1, 2, 3, 5, 8, 13, 21, ........
each number is the sum of the two preceding numbers.
Now see the program
#include<stdio.h>
#include<conio.h>
int fibnum(int);
int main()
{
int position ,result;
printf("Enter the position of element you want to print from Fibonacci series\t");
scanf("%d",&position);
result = fibnum(position);
printf("The required fibonacci number is = %d",result);
}
int fibnum(int n)
{
if(n == 0 || n == 1)
{
return(n);
}
else
{
return(fibnum(n - 1) + fibnum(n - 2));
}
}
Output:
Enter the position of element you want to print from Fibonacci series 7
The required Fibonacci number is = 13.
Do not forget to subscribe me for latest update on my blog , follow me and grow your knowledge. thank you
0 comments:
Post a Comment