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.
#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.
Output of the Fibonacci Sequence Program
When you run the program, here's what you should see:
Enter the number of terms: 5
Fibonacci Sequence: 0, 1, 1, 2, 3
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.
- Read the number of terms
nfrom the user. - Initialize the first two terms
t1andt2as 0 and 1. - Print
t1andt2. - Repeat the following until
ireachesn: - Calculate the next term as
nextTerm = t1 + t2. - Print
nextTerm. - Update
t1ast2andt2asnextTerm.
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])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 forprintfandscanf.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 ton.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 untilnis reached.nextTerm = t1 + t2;: Calculates each subsequent term by adding the previous two terms.printf("%d", nextTerm);: Prints the newly calculated term.t1 = t2;andt2 = nextTerm;: Updatest1andt2for the next iteration.return 0;: Ends the main function.
Quick Quiz — test your understanding
Tap an option to check your answerWhat is the time complexity of generating the Fibonacci sequence up to n terms?
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.
Related Programs
If you found this program helpful, check out these related programs:
- C Program to Reverse a Number with Example
- C Program to Multiply Two Floating-Point Numbers
- C Program to Compute Quotient and Remainder Easily
- C Program to Generate Multiplication Table with Example
- C Program to Count Number of Digits in an Integer
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.