C Program to Find Nth Fibonacci Number using Recursion

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.

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.

  1. /*
  2.  * C Program to find the nth number in Fibonacci series using recursion
  3.  */
  4. #include <stdio.h>
  5. int fibo(int);
  6.  
  7. int main()
  8. {
  9.     int num;
  10.     int result;
  11.  
  12.     printf("Enter the nth number in fibonacci series: ");
  13.     scanf("%d", &num);
  14.     if (num < 0)
  15.     {
  16.         printf("Fibonacci of negative number is not possible.\n");
  17.     }
  18.     else
  19.     {
  20.         result = fibo(num);
  21.         printf("The %d number in fibonacci series is %d\n", num, result);
  22.     }
  23.     return 0;
  24. }
  25. int fibo(int num)
  26. {
  27.     if (num == 0)
  28.     {
  29.         return 0;
  30.     }
  31.     else if (num == 1)
  32.     {
  33.         return 1;
  34.     }
  35.     else
  36.     {
  37.         return(fibo(num - 1) + fibo(num - 2));
  38.     }
  39. }

$ 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.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.