✓ Link copied to clipboard
C 📅 July 4, 2026 ⏱ 2 min read

C Program to Display Fibonacci Sequence with Example

C Program to Display Fibonacci Sequence with Example

Fibonacci numbers pop up everywhere in nature. From how leaves are arranged to the pattern on a pineapple, you’ll find these numbers often. In this program, we’ll generate the Fibonacci sequence up to a specific number of terms. For example, if you enter 5, the sequence will be: 0, 1, 1, 2, 3.

Prerequisites for the Fibonacci Sequence Program in C

Make sure you're familiar with these concepts before diving into the code.

  • Understanding of loops (for loop)
  • Basic knowledge of variables, especially int
  • How to print to the console using printf()
  • <stdio.h> for standard I/O functions
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler

Source Code of C Program to Display Fibonacci Sequence

Here's the complete code for generating the Fibonacci sequence in C.

fibonacci.cC▶ Run
#include <stdio.h>

int main() {
    int n, t1 = 0, t2 = 1, nextTerm;
    
    printf("Enter the number of terms: ");
    scanf("%d", &n);

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

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

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Fibonacci Sequence Program

When you run the program, here's what you should see:

Output
Enter the number of terms: 5
Fibonacci Sequence: 0, 1, 1, 2, 3
Output of C Program to Display Fibonacci Sequence
Output of C Program to Display Fibonacci Sequence

Algorithm to Display Fibonacci Sequence in C

The Fibonacci sequence relies on summing the two previous numbers to get the next one. Here's a step-by-step guide to the logic this program follows.

  1. Read the number of terms n from the user.
  2. Initialize the first two terms t1 and t2 as 0 and 1.
  3. Print t1 and t2.
  4. Repeat the following until i reaches n:
  5. Calculate the next term as nextTerm = t1 + t2.
  6. Print nextTerm.
  7. Update t1 as t2 and t2 as nextTerm.
flowchart TD
  A([Start]) --> B[/Read n/]
  B --> C[Initialize: t1 = 0, t2 = 1]
  C --> D[Print t1 and t2]
  D --> E[i = 3]
  E --> F{"i <= n?"}
  F -->|Yes| G["nextTerm = t1 + t2"]
  G --> H[Print nextTerm]
  H --> I[t1 = t2]
  I --> J[t2 = nextTerm]
  J --> K[i = i + 1]
  K --> F
  F -->|No| L([End])
Flowchart: the algorithm step by step

How the Fibonacci Sequence Program Works

Let's break down how each part of the code functions:

  • #include <stdio.h>: Includes the standard input-output library necessary for printf and scanf.
  • int main(): The starting point of our program.
  • int n, t1 = 0, t2 = 1, nextTerm;: Declares the integers used for the sequence and initializes the first two terms.
  • printf("Enter the number of terms: ");: Prompts the user to input the number of terms.
  • scanf("%d", &n);: Reads the user's input and assigns it to n.
  • printf("Fibonacci Sequence: %d, %d, ", t1, t2);: Prints the first two terms of the sequence.
  • for (int i = 3; i <= n; ++i): A loop starting from the third term that runs until n is reached.
  • nextTerm = t1 + t2;: Calculates each subsequent term by adding the previous two terms.
  • printf("%d", nextTerm);: Prints the newly calculated term.
  • t1 = t2; and t2 = nextTerm;: Updates t1 and t2 for the next iteration.
  • return 0;: Ends the main function.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What is the time complexity of generating the Fibonacci sequence up to n terms?

🎉Correct! The loop runs n-2 times, making it O(n).
🤔Not quite. Think about how many iterations the loop runs.

Time and Space Complexity of the Fibonacci Sequence Program in C

Understanding the efficiency of this Fibonacci sequence program is crucial.

Time Complexity: O(n). The program uses a single loop that iterates from 3 up to n, making the complexity O(n). Here, n refers to the number of terms you want in the sequence — 5 for 5 terms. This means if you want 10 terms, the loop runs 10-2=8 times.

Space Complexity: O(1). This program only requires a fixed amount of memory to store the variables t1, t2, nextTerm, and n. These occupy the same space regardless of how many terms you choose.

⚠️

Common mistake: Forgetting to update t1 and t2 in each loop iteration will result in an incorrect sequence.

Key takeaway: The Fibonacci sequence is simple with loops, but don’t forget to initialize correctly.

FAQs About C Program to Display Fibonacci Sequence

These are some common questions about this program.

The Fibonacci sequence is a set of numbers where each term is the sum of the two before it, usually starting with 0 and 1.
The first five Fibonacci numbers are 0, 1, 1, 2, and 3.
Fibonacci numbers are generally non-negative integers, starting from 0 and going up.
To calculate the nth Fibonacci number in C, you can use a loop to iteratively add the two previous numbers up to the nth term.
The sequence starts with 0 and 1, since these create the base for the next terms.
Yeah, it's a popular topic for coding interviews due to its math properties and how it can be implemented.

If you found this program helpful, check out these related programs:

Conclusion

Making a Fibonacci sequence generator is a great way to practice loops and conditional logic in C. Check out different ways to optimize it, like using arrays for bigger sequences. You could also try adapting this logic to other math sequences. You might see patterns you never thought about.

Want to test your skills? Take a quiz to boost your understanding, or dive deeper in the EduSeekho online compiler.