C Program to Print Fibonacci Series using Recursion

This program prints the Fibonacci series using recursion in C. It demonstrates how recursive functions can be used to generate a mathematical sequence efficiently.

Concept Overview

In recursion, a function calls itself until a base condition is met. The Fibonacci series is a classic example where each term is the sum of the two preceding ones.

Program

C
#include <stdio.h>

// Recursive function to generate Fibonacci number
int fibonacci(int n) {
    if(n == 0) return 0;
    else if(n == 1) return 1;
    else return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
    int n;

    printf("Enter number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series:\n");
    for(int i = 0; i < n; i++) {
        printf("%d\n", fibonacci(i));
    }

    return 0;
}

Output

0
1
1
2
3
5
8
13
21
34

Explanation

  • The function `fibonacci()` returns 0 if `i` is 0, and 1 if `i` is 1 — these are the base cases.
  • For any other value of `i`, it returns the sum of the previous two Fibonacci numbers using recursion: `fibonacci(i - 1) + fibonacci(i - 2)`.
  • The loop in `main()` prints the first 10 Fibonacci numbers by calling the recursive function repeatedly.
  • This demonstrates how recursion simplifies problems that can be defined in terms of smaller subproblems.
Note: Be cautious when using recursion for large values — it may lead to high memory usage or stack overflow. Use iterative methods for better performance in such cases.