C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Generate Fibonacci series up to N terms

Introduction

The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones.
It starts with 0 and 1, and continues as:

 C Programs

C Program: Generate Fibonacci series up to N terms

NOTE: The Program is written using for, while and do..while loop.
Method 1 is for loop, Method 2 is while loop and Method 3 is do..while loop

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int n, i;

    long long first = 0, second = 1, next;

 

    // Input

    printf("Enter the number of terms: ");

    scanf("%d", &n);

 

    // Validate input

    if (n <= 0) {

        printf("Please enter a positive integer.\n");

    } else {

        printf("Fibonacci Series up to %d terms:\n", n);

 

        for (i = 1; i <= n; i++) {

            printf("%lld ", first);

            next = first + second;

            first = second;

            second = next;

        }

        printf("\n");

    }

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter the number of terms: 10
Fibonacci Series up to 10 terms:
0 1 1 2 3 5 8 13 21 34

Explanation

  1. The user enters the number of terms n.
  2. The first two terms are initialized as 0 and 1.
  3. A for loop runs n times:
    • Prints the current term (first)
    • Calculates the next term as next = first + second
    • Updates first and second
  4. The loop continues until all terms are printed.

 

C Program: Generate Fibonacci series up to N terms

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int n, i = 1;

    long long first = 0, second = 1, next;

 

    // Input number of terms

    printf("Enter the number of terms: ");

    scanf("%d", &n);

 

    // Validate input

    if (n <= 0) {

        printf("Please enter a positive integer.\n");

    } else {

        printf("Fibonacci Series up to %d terms:\n", n);

 

        while (i <= n) {

            printf("%lld ", first);

            next = first + second;

            first = second;

            second = next;

            i++;

        }

        printf("\n");

    }

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter the number of terms: 8
Fibonacci Series up to 8 terms:
0 1 1 2 3 5 8 13


Explanation

  1. The program starts by reading the number of terms n.
  2. It initializes the first two Fibonacci numbers:
    first = 0, second = 1.
  3. The while loop continues until i reaches n.
  4. In each iteration:
    • Print the current term.
    • Compute the next term as next = first + second.
    • Update first and second.
  5. When i > n, the loop stops.

 

C Program: Generate Fibonacci series up to N terms

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int n, i = 1;

    long long first = 0, second = 1, next;

 

    // Input number of terms

    printf("Enter the number of terms: ");

    scanf("%d", &n);

 

    // Validate input

    if (n <= 0) {

        printf("Please enter a positive integer.\n");

    } else {

        printf("Fibonacci Series up to %d terms:\n", n);

 

        do {

            printf("%lld ", first);

            next = first + second;

            first = second;

            second = next;

            i++;

        } while (i <= n);

 

        printf("\n");

    }

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter the number of terms: 10
Fibonacci Series up to 10 terms:
0 1 1 2 3 5 8 13 21 34

Explanation

  1. Initialize:
    • first = 0, second = 1, i = 1.
  2. The do block executes at least once:
    • Prints first.
    • Computes the next Fibonacci number as next = first + second.
    • Updates first and second.
    • Increments i.
  3. The loop continues while i <= n.
  4. The sequence stops after printing N