Fibonacci series in C using recursion

(Last Updated On: 13/12/2023)

Dive into the fascinating world of the Fibonacci series, a cool math sequence! Ever wondered how it magically appears in code? This blog explores a special way – using recursion. Imagine a function that talks to itself, helping create the Fibonacci series. We’ll uncover this cool concept called recursion, where functions can call themselves, and we’ll learn a trick to avoid talking forever by setting a base case. Get ready for a coding adventure!

// Fibonacci series in c using recursion
#include<stdio.h>
int Fibonacci(int);
int main()
{
    int num, i = 0, c;
    scanf("%d",&num);
    printf("Fibonacci series:\n");
    for ( c = 1 ; c <= num ; c++ ){
        printf("%d\n", Fibonacci(i));
        i++;
    }
    return 0;
}
int Fibonacci(int num){
    if ( num == 0 )
    return 0;
    else if ( num == 1 )
    return 1;
    else
    return ( Fibonacci(num-1) + Fibonacci(num-2) );
}

Explore the fascinating world of Fibonacci series in C using while loop Check out!

Explanation of the Code:

This C program generates the Fibonacci series using recursion. The `main` function prompts the user to input the number of terms in the series (`num`). 

It then iterates through a loop, printing each term of the Fibonacci series by calling the `Fibonacci` function. 

The `Fibonacci` function defines the base cases (returning 0 for `num` equal to 0 and 1 for `num` equal to 1) and recursively calculates the subsequent terms. 

The program continues until the desired number of terms is printed. Recursion allows for a concise and elegant implementation, breaking down the problem into smaller, solvable components.

Learn How to Generate Fibonacci series in C using for loop, Now!

Output:

Enter the number of terms in the Fibonacci series: 5
Fibonacci series up to 5 terms:
0 1 1 2 3

Comparison with Other Methods

RecursionFor and While Loop Implementations
AdvantagesConciseness: Recursive code is often more compact and expressive.Mathematical Representation: Mirrors the mathematical definition of the Fibonacci series.Flexibility: Adaptable for various recursive problems.Simplicity: Loop structures provide a straightforward way to iterate and generate the Fibonacci series.Readability: Code is often concise and easy to understand.Direct Control: Explicit control over initialization, condition, and iteration.
ConsiderationsStack Usage: Recursion utilizes the call stack, potentially leading to stack overflow for large input values.Execution Time: Recursive solutions might be less efficient for large Fibonacci series due to repeated calculations.Fixed Iterations: The number of iterations must be predetermined, limiting flexibility.Code Length: Can be longer compared to recursive solutions.

Know the Fibonacci Series in Python Using For Loop here!

In conclusion, exploring the Fibonacci series through recursion adds a touch of elegance to the coding journey. While for and while loops offer simplicity, recursion captivates with its concise representation. Each method brings its unique strengths, underscoring the beauty of choice in solving mathematical problems in the realm of programming.  

Ready to see your code come to life? Give it a go on the Newtum Online Compiler! Just click, and start exploring the endless possibilities of coding. Learn, experiment, and execute your code effortlessly with Newtum – the perfect platform for coding enthusiasts!

About The Author