In this c program, we will let you know how to find the Nth fibonacci number using recursion. In fibonacci series, each number is the sum of the two previous numbers. below is the example of fibonacci series.
1 | 0, 1, 1, 2, 3, 5, 8,... |
Copy the below c program and execute it using the c compiler to print the nth number of a Fibonacci number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #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)); } } |
Enter the nth number in fibonacci series: 8
The 8 number in fibonacci series is 21
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.