THE SYLLABUS QUESTION
What you need to solve
A Fibonacci sequence is defined as follows: the first and second terms are 0 and 1. Subsequent terms are found by adding the preceding two terms. Write a C program to generate the first n terms of the sequence.
The number of terms, from 1 to 93. This limit avoids overflowing unsigned long long in the values generated by this implementation.
UNDERSTAND THE IDEA
Explanation
Keep two consecutive terms: current starts at 0 and next starts at 1. Print current, then move both values forward.
The sequence here starts with F0 = 0. Thus the first 93 terms end at F92. Avoid calculating an extra term after printing the final requested value.
PLAN BEFORE CODING
Algorithm
- Read and validate the requested term count.
- Initialize current = 0 and next = 1.
- Print current.
- Unless this was the final term, calculate a temporary sum and shift the two values.
- Repeat until n terms are printed.
SEE THE CONTROL FLOW
Flowchart
Follow the arrows from Start. Diamonds ask a question; labeled arrows show the answer. A returning arrow repeats a loop. Function internals are grouped where needed; later input checks follow the rules in the program.
On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗
#include <stdio.h>
int main(void) {
int n;
unsigned long long current = 0, next = 1;
if (scanf("%d", &n) != 1 || n < 1 || n > 93) {
puts("Invalid input.");
return 1;
}
for (int i = 0; i < n; ++i) {
printf("%s%llu", i == 0 ? "" : " ", current);
if (i < n - 1) {
unsigned long long sum = current + next;
current = next;
next = sum;
}
}
putchar('\n');
return 0;
}
Code loads into the existing compiler. Enter the sample input there; sign-in and execution rules stay the same.
Compile and run locally
gcc -std=c17 fibonacci-sequence.c -o lab
./labOn Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.
FOLLOW THE VALUES
Dry run
| Step / state | Operation | Result |
|---|---|---|
| Before term 1 | current = 0; next = 1 | Print 0 |
| Before term 2 | current = 1; next = 1 | Print 1 |
| Before term 3 | current = 1; next = 2 | Print 1 |
| Before term 4 | current = 2; next = 3 | Print 2 |
CHECK THE BEHAVIOR
Sample input & output
Each output below was produced by compiling and running this exact program. Input values are entered in the stated order; the examples do not print input prompts.
Sample 1
7
0 1 1 2 3 5 8
Sample 2
1
0
Sample 3
2
0 1
Common mistakes
- Use a temporary sum so one update does not destroy a value still needed.
- Do not print n + 1 terms or generate beyond the supported numeric range.
WHY THIS GROWTH RATE?
Time and space complexity
Time O(n); auxiliary space O(1).
Let n be the requested number of terms. The program prints each term once and updates two running values using one addition. It does not recursively recompute earlier terms.
There are n output iterations, so time is O(n). Only current, next, a temporary sum and counters are retained: O(1) auxiliary space. The 93-term bound prevents integer overflow; the estimate describes growth with the requested count.
Big-O describes how work grows as the stated input quantity grows; fixed factors and lower-order terms are omitted. The analysis treats fixed-width arithmetic as constant cost and the published limits as practical safety bounds.