Generate Fibonacci Series using Loops in C

To generate the Fibonacci series in C, we use loops such as for, while, or do-while to compute the sequence iteratively. The Fibonacci series starts with 0 and 1, and each subsequent term is the sum of the previous two terms. This tutorial demonstrates multiple ways to generate the Fibonacci sequence using loops in C.


Examples of Generating Fibonacci Series in C

1. Generating Fibonacci Series Using a for Loop

In this example, we will use a for loop to generate the Fibonacci series up to a given number of terms.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n, first = 0, second = 1, next;
    
    printf("Enter the number of terms: ");
    scanf("%d", &n);

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

    return 0;
}

Explanation:

  1. We declare three variables: first (initialized to 0), second (initialized to 1), and next (to store the sum of the previous two terms).
  2. We take user input for the number of terms (n).
  3. We use a for loop to generate the Fibonacci series for n terms.
  4. In each iteration, we print first, compute next as the sum of first and second, then update first and second.

Output (Example for n = 5):

Enter the number of terms: 5
Fibonacci Series: 0 1 1 2 3

2. Generating Fibonacci Series Using a while Loop

In this example, we will use a while loop to generate the Fibonacci sequence until we reach the required number of terms.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n, first = 0, second = 1, next, count = 0;
    
    printf("Enter the number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series: ");
    while (count < n) {
        printf("%d ", first);
        next = first + second;
        first = second;
        second = next;
        count++;
    }

    return 0;
}

Explanation:

  1. We declare a count variable to track iterations.
  2. We initialize first and second to 0 and 1.
  3. We take user input for the number of terms (n).
  4. The while loop continues until count reaches n.
  5. Each iteration prints first, computes next, and updates first and second.

Output (Example for n = 6):

Enter the number of terms: 6
Fibonacci Series: 0 1 1 2 3 5

3. Generating Fibonacci Series Using a do-while Loop

In this example, we use a do-while loop to generate the Fibonacci series. This ensures that at least one term is printed even if the user enters 1 as the number of terms.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n, first = 0, second = 1, next, count = 0;
    
    printf("Enter the number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series: ");
    do {
        printf("%d ", first);
        next = first + second;
        first = second;
        second = next;
        count++;
    } while (count < n);

    return 0;
}

Explanation:

  1. We declare and initialize first, second, and next to generate the sequence.
  2. We take user input for the number of terms (n).
  3. The do-while loop executes at least once, ensuring the first term is always printed.
  4. The loop continues generating and printing Fibonacci numbers until count reaches n.

Output (Example for n = 4):

Enter the number of terms: 4
Fibonacci Series: 0 1 1 2

Conclusion

In this tutorial, we explored how to generate Fibonacci series using loops in C:

  1. for Loop: Best when the number of iterations is known.
  2. while Loop: Useful when a condition-based iteration is required.
  3. do-while Loop: Ensures at least one iteration.