C Program to Print the Fibonacci Series
C Program to Print the Fibonacci Series
A number-sequence exercise grows each term from the preceding two. Saving the new sum before replacing either term prevents a corrupted sequence.
Starting from zero and one, each Fibonacci term after them is the sum of the two preceding terms.
Before this exercise, review repeating work with loops. The worked program below applies those ideas to this specific problem.
Explain
Explain why fibonacci sequence uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Print the first five terms instead of seven.
Fibonacci Sequence: follow the data
Given data
A number-sequence exercise grows each term from the preceding two. Saving the new sum before replacing either term prevents a corrupted sequence.
Rule
Starting from zero and one, each Fibonacci term after them is the sum of the two preceding terms.
Observed result
0 1 1 2 3 5 8
Understand the operation
a holds the term to print; b holds the following term. Compute next = a + b before overwriting a or b.
The example asks for a term count, not a maximum term value. Printing seven terms includes the initial zero as the first term.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
unsigned a = 0, b = 1;
for (int i = 0; i < 7; i++) {
printf("%u ", a);
unsigned next = a + b;
a = b; b = next;
}
putchar('\n');
return 0;
}
Expected output
0 1 1 2 3 5 8
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
unsigned a = 0, b = 1;
for (int i = 0; i < 7; i++) {
printf("%u ", a);
unsigned next = a + b;
a = b; b = next;
}
putchar('\n');
return 0;
}
The initial pair is (0, 1).
After printing zero, next is one and the pair becomes (1, 1). Later pairs produce two, three, five and eight.
Follow the worked calculation
Start with the sample
The initial pair is (0, 1).
Apply the rule
After printing zero, next is one and the pair becomes (1, 1). Later pairs produce two, three, five and eight.
Check the result
0 1 1 2 3 5 8
0 1 1 2 3 5 8
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
unsigned a = 0, b = 1;
for (int i = 0; i < 7; i++) {
printf("%u ", a);
unsigned next = a + b;
a = b; b = next;
}
putchar('\n');
return 0;
}
Print the first five terms instead of seven.
Worked answer
Only the iteration count changes. The two-value update rule still prints zero, one, one, two and three.
C17 · complete program
#include <stdio.h>
int main(void) {
unsigned a = 0, b = 1;
for (int i = 0; i < 5; i++) {
printf("%u ", a);
unsigned next = a + b;
a = b; b = next;
}
putchar('\n');
return 0;
}
Expected output
0 1 1 2 3
Run in C compiler →Recognize the important boundaries
The sequence grows quickly. Check a + b for overflow before updating when the term count is not a small fixed fixture.
Define whether your exercise starts with 0,1 or 1,1; otherwise an apparently different output may use a different convention.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Starting from zero and one, each Fibonacci term after them is the sum of the two preceding terms.
Continue with testing the problem’s boundary cases.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.