C Program to Display the Fibonacci Sequence

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1. This article will cover various C Program to Display the Fibonacci Sequence. We will explore multiple examples, each demonstrating a different approach to solve the problem. Additionally, we will discuss the prerequisites, provide detailed explanations for each example, and conclude with a summary of what we’ve learned.

Prerequisites

Before we delve into the examples, ensure you have the following prerequisites:

  • A C compiler (such as GCC)
  • A text editor or IDE for writing your C code
  • Basic understanding of C programming concepts, especially loops and functions

1. Generating the Fibonacci Sequence

In this section, we will look at several methods to generate and display the Fibonacci sequence in C.

1.1 Using a for Loop

Example 1: Generate Fibonacci Sequence Using a for Loop

This method uses a for loop to generate and display the Fibonacci sequence iteratively.

Code

C
#include <stdio.h>

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

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

    printf("Fibonacci Sequence: ");

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

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Input the number of terms: scanf reads the number of terms from the user.
  • Generate the sequence using a for loop: The loop runs for n terms, updating and printing each term.
  • Output the result: printf displays the Fibonacci sequence.

Output

C
Enter the number of terms: 10
Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

1.2 Using a while Loop

Example 2: Generate Fibonacci Sequence Using a while Loop

This method uses a while loop to generate and display the Fibonacci sequence iteratively.

Code

C
#include <stdio.h>

int main() {
    int n, t1 = 0, t2 = 1, nextTerm, i = 1;

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

    printf("Fibonacci Sequence: ");

    while (i <= n) {
        printf("%d, ", t1);
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
        ++i;
    }

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Input the number of terms: scanf reads the number of terms from the user.
  • Generate the sequence using a while loop: The loop runs for n terms, updating and printing each term.
  • Output the result: printf displays the Fibonacci sequence.

Output

C
Enter the number of terms: 7
Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8,

1.3 Using Recursion

Example 3: Generate Fibonacci Sequence Using Recursion

This method uses recursion to generate and display the Fibonacci sequence.

Code

C
#include <stdio.h>

int fibonacci(int n);

int main() {
    int n;

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

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

    return 0;
}

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

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Declare a recursive function: int fibonacci(int n) to compute the nth Fibonacci number.
  • Input the number of terms: scanf reads the number of terms from the user.
  • Generate the sequence using recursion: A for loop calls the recursive function for each term and prints it.
  • Output the result: printf displays the Fibonacci sequence.

Output

C
Enter the number of terms: 6
Fibonacci Sequence: 0, 1, 1, 2, 3, 5,

1.4 Using Dynamic Programming

Example 4: Generate Fibonacci Sequence Using Dynamic Programming

This method uses dynamic programming to generate the Fibonacci sequence efficiently.

Code

C
#include <stdio.h>

void fibonacci(int n);

int main() {
    int n;

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

    printf("Fibonacci Sequence: ");
    fibonacci(n);

    return 0;
}

void fibonacci(int n) {
    int f[n];
    f[0] = 0;
    f[1] = 1;

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

    for (int i = 0; i < n; ++i) {
        printf("%d, ", f[i]);
    }
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Declare a function: void fibonacci(int n) to compute and print the Fibonacci sequence.
  • Input the number of terms: scanf reads the number of terms from the user.
  • Generate the sequence using dynamic programming: Store computed values in an array to avoid redundant calculations.
  • Output the result: printf displays the Fibonacci sequence.

Output

C
Enter the number of terms: 8
Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13,

1.5 Using Arrays

Example 5: Generate Fibonacci Sequence Using Arrays

This method stores the Fibonacci sequence in an array and prints it.

Code

C
#include <stdio.h>

int main() {
    int n;

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

    int f[n];
    f[0] = 0;
    f[1] = 1;

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

    printf("Fibonacci Sequence: ");
    for (int i = 0; i < n; ++i) {
        printf("%d, ", f[i]);
    }

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Input the number of terms: scanf reads the number of terms from the user.
  • Store the sequence in an array: Compute the Fibonacci sequence and store it in an array.
  • Print the sequence: A loop iterates over the array to print the Fibonacci sequence.
  • Output the result: printf displays the Fibonacci sequence.

Output

C
Enter the number of terms: 9
Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21,

2. Conclusion

In this article, we explored various methods to generate and display the Fibonacci sequence in C: using a for loop, using a while loop, using recursion, using dynamic programming, and using arrays. Each method demonstrates different aspects of handling loops, functions, and arrays in C programming. By understanding these methods, you can choose the one that best fits your specific needs and enhance your skills in basic arithmetic operations and programming logic in C.