This C Program prints the fibonacci of a given number using recursion. In fibonacci series, each number is the sum of the two preceding numbers. Eg: 0, 1, 1, 2, 3, 5, 8, …
The following program returns the nth number entered by user residing in the fibonacci series.
The following program returns the nth number entered by user residing in the fibonacci series.
Here is the source code of the C program to print the nth number of a fibonacci number. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to find the nth number in Fibonacci series using recursion
*/
#include <stdio.h>
int fibo(int);
int main()
{
int num;
int result;
printf("Enter the nth number in fibonacci series: ");
scanf("%d", &num);
if (num < 0)
{
printf("Fibonacci of negative number is not possible.\n");
}
else
{
result = fibo(num);
printf("The %d number in fibonacci series is %d\n", num, result);
}
return 0;
}
int fibo(int num)
{
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
return(fibo(num - 1) + fibo(num - 2));
}
}
$ cc pgm9.c $ a.out Enter the nth number in fibonacci series: 8 The 8 number in fibonacci series is 21 $ a.out Enter the nth number in fibonacci series: 12 The 12 number in fibonacci series is 144
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy C Books
- Apply for C Internship
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Practice BCA MCQs