Fibonacci Series Using Recursion in C

Here, we will write a program to find the Fibonacci series using recursion in C language, and also we will find the nth term of the Fibonacci series. Prerequisites:- Recursion in C Programming Language

Another example of recursion is a function that generates Fibonacci numbers. Named after an Italian mathematician, Leonardo Fibonacci, who lived in the early thirteenth century. Fibonacci numbers are a series in which each number is the sum of the previous two numbers.

In the Fibonacci series, the next element is the sum of the previous two elements. The Fibonacci sequence is a series of numbers where a number is found by adding up the two numbers before it.

Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so forth. Written as a rule, the expression is Xn= Xn-1+ Xn-2

By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

The base case for finding factorial
fibonacci(0) = 0
fibonacci(1) = 1
General case for finding factorial
fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)

Algorithm:- fibonacci(n)

if (n<=1) 
then return n
else 
return fibonacci(n-1) + fibonacci(n-2)

C program to Find Nth Fibonacci term using Recursion

Program description:- Write a C program to find the nth Fibonacci number using recursion techniques.

#include<stdio.h>

// recursive function for finding nth Fibonacci term
int fibonacci(int n)
{
      if(n<=0)
          return n; //base case
      else //general case
          return fibonacci(n-1)+fibonacci(n-2);
}

int main()
{
     int num;
     printf("Which term of Fibonacci series
                    you want to find: ");
     scanf("%d",&num);
     printf("%dth term is %d\n",num,
                        fibonacci(num));
     return 0;
}

Output for the different test-cases:-

Which term of Fibonacci series you want to find: 5
5th term is 5

Which term of Fibonacci series you want to find: 7
7th term is 13

Which term of Fibonacci series you want to find: 10
10th term is 55


Find first N numbers in Fibonacci series

Program description:- Write a C program to display first n numbers in the Fibonacci series using recursion in C.

#include<stdio.h>
int fibonacci(int n)
{
    if(n<=0)
       return n; //base case
    else //general case
       return fibonacci(n-1)+fibonacci(n-2);
}
int main()
{
     int num;
     printf("How many numbers of Fibonacci
                series you want to print: ");
     scanf("%d",&num);
     printf("Fibonacci series:\n");
     for(int i=0; i<num; i++)
     {
         printf("%d\t",fibonacci(i));
     }
     return 0;
}

Output for the different test-cases:-

How many numbers you want to print: 5
Fibonacci series:
0 1 1 2 3

How many numbers you want to print: 10
Fibonacci series:
0 1 1 2 3 5 8 13 21 34

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *