C Fibonacci Series Using Recursion Program


The "Fibonacci Series Using Recursion" program in C generates the Fibonacci series up to a specified number of terms using a recursive function. The Fibonacci series is a sequence where each number is the sum of the two preceding ones, usually starting with 0 and 1. The series looks like this:

0,1,1,2,3,5,8,13,21,34,0, 1, 1, 2, 3, 5, 8, 13, 21, 34, \ldots

Fibonacci Sequence Definition

The Fibonacci sequence can be defined recursively as follows:

  • F(0)=0F(0) = 0
  • F(1)=1F(1) = 1
  • F(n)=F(n1)+F(n2)F(n) = F(n-1) + F(n-2) for n>1n > 1

Implementation of Fibonacci Series Using Recursion

Here’s a simple implementation of the Fibonacci series program using recursion in C:

#include <stdio.h> // Function to calculate Fibonacci number recursively int fibonacci(int n) { // Base cases if (n == 0) { return 0; } if (n == 1) { return 1; } // Recursive case return fibonacci(n - 1) + fibonacci(n - 2); } int main() { int terms, i; printf("Enter the number of terms in the Fibonacci series: "); scanf("%d", &terms); // Read input from user printf("Fibonacci Series: "); for (i = 0; i < terms; i++) { printf("%d ", fibonacci(i)); // Print each Fibonacci number } printf("\n"); return 0; }

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: This header is included for standard input and output functions.
  2. Function Declaration:

    • int fibonacci(int n): This function takes an integer n and returns the nthn^{th} Fibonacci number.
  3. Base Cases:

    • The base cases check if n is 0 or 1. If n is 0, it returns 0, and if n is 1, it returns 1.
  4. Recursive Case:

    • If n is greater than 1, the function calls itself twice: once with n - 1 and once with n - 2, and returns the sum of these two calls. This builds the Fibonacci sequence by calculating previous terms recursively.
  5. Main Function:

    • The program starts by prompting the user to enter the number of terms for the Fibonacci series.
    • It uses scanf() to read the input value.
  6. Generating the Series:

    • A loop runs from 0 to the specified number of terms. For each iteration, it calls the fibonacci() function and prints the result.
  7. Output:

    • The program prints the Fibonacci series up to the specified number of terms.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code.

    gcc fibonacci_recursive.c -o fibonacci_recursive
  2. Execute the Program:

    ./fibonacci_recursive
  3. Input Data: Enter the number of terms when prompted.

Example Input/Output

Input:

Enter the number of terms in the Fibonacci series: 10

Output:

Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

Conclusion

The "Fibonacci Series Using Recursion" program in C demonstrates how recursion can be applied to generate sequences. While this implementation is straightforward and easy to understand, it is important to note that this recursive approach has exponential time complexity due to overlapping subproblems. For larger values of n, an iterative or memoized approach would be more efficient. Nonetheless, this program serves as a good introduction to recursion in C programming.