Fibonacci series in C

The Fibonacci series is a sequence where each number is the sum of the two preceding ones, usually starting with 0 and 1. It is a fundamental concept in computer science and programming. In this article, we will explore three different methods to print the Fibonacci series in C, providing detailed explanations and code examples for each approach.

Three different methods to write a program to print the “Fibonacci series in C”

Method 1) Program to print the “Fibonacci series in C” using an Iterative Approach

Introduction

The iterative method is one of the simplest and most efficient ways to generate the Fibonacci series. It uses a loop to calculate each term by summing the two previous terms.

Code Example

#include <stdio.h>

void printFibonacciIterative(int n) {
    int t1 = 0, t2 = 1, nextTerm;

    printf("Fibonacci Series: %d, %d", t1, t2);

    for (int i = 1; i <= n - 2; ++i) {
        nextTerm = t1 + t2;
        printf(", %d", nextTerm);
        t1 = t2;
        t2 = nextTerm;
    }
}

int main() {
    int n;
    printf("Enter the number of terms: ");
    scanf("%d", &n);
    printFibonacciIterative(n);
    return 0;
}

Explanation of the Code

  1. Initialization: Start with the first two terms, t1 = 0 and t2 = 1.
  2. Loop: Iterate from 1 to n-2 to calculate the next terms by adding the two preceding terms.
  3. Output: Print each term as it is calculated.

This method is straightforward and efficient, making it suitable for generating a large number of Fibonacci terms.

Method 2) Program to print the “Fibonacci series in C” using a Recursive Approach

Introduction

The recursive method leverages the natural definition of the Fibonacci series, where each term is defined as the sum of the two preceding terms. This method is elegant but less efficient for large sequences due to repeated calculations.

Code Example

#include <stdio.h>

int fibonacciRecursive(int n) {
    if (n <= 1)
        return n;
    return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}

void printFibonacciRecursive(int n) {
    printf("Fibonacci Series: ");
    for (int i = 0; i < n; ++i) {
        printf("%d", fibonacciRecursive(i));
        if (i != n - 1) printf(", ");
    }
}

int main() {
    int n;
    printf("Enter the number of terms: ");
    scanf("%d", &n);
    printFibonacciRecursive(n);
    return 0;
}

Explanation of the Code

  1. Base Case: If n is 0 or 1, return n.
  2. Recursive Case: Calculate the term as the sum of the previous two terms.
  3. Output: Print each term by calling the recursive function.

While the recursive approach is intuitive and easy to understand, it can be inefficient due to the exponential time complexity.

Method 3) Program to print the “Fibonacci series in C” using a Dynamic Programming Approach

Introduction

Dynamic programming optimizes the recursive method by storing previously computed terms in an array, avoiding redundant calculations. This method is efficient and avoids the drawbacks of the simple recursive approach.

Code Example

#include <stdio.h>

void printFibonacciDP(int n) {
    int fib[n];
    fib[0] = 0;
    fib[1] = 1;

    for (int i = 2; i < n; ++i) {
        fib[i] = fib[i - 1] + fib[i - 2];
    }

    printf("Fibonacci Series: ");
    for (int i = 0; i < n; ++i) {
        printf("%d", fib[i]);
        if (i != n - 1) printf(", ");
    }
}

int main() {
    int n;
    printf("Enter the number of terms: ");
    scanf("%d", &n);
    printFibonacciDP(n);
    return 0;
}

Explanation of the Code

  1. Array Initialization: Create an array fib to store Fibonacci terms.
  2. Base Cases: Initialize the first two terms as fib[0] = 0 and fib[1] = 1.
  3. Loop: Fill the array by summing the previous two terms.
  4. Output: Print the series from the array.

The dynamic programming approach is highly efficient, with a linear time complexity, making it ideal for generating large Fibonacci sequences.

In this article, we explored three different methods to generate the Fibonacci series in C: the iterative approach, the recursive approach, and the dynamic programming approach. Each method has its advantages and use cases. The iterative method is straightforward and efficient, the recursive method is intuitive but less efficient, and the dynamic programming approach offers the best performance for large sequences. Understanding these methods provides a solid foundation for tackling various problems involving the Fibonacci series and similar recursive sequences.

Categories C

Leave a Comment