Start with the iterative loop to print n terms. Use a small recursive helper only to show the recurrence for one term.
Example 1 — Iterative Series
Keep two previous values and print n terms with a loop.
C
#include <stdio.h>
int main(void)
{
int n = 10;
long long a = 0, b = 1, next;
int i;
if (n < 1)
{
printf("Enter n >= 1\\n");
return 1;
}
for (i = 1; i <= n; i++)
{
printf("%lld", a);
if (i < n)
{
printf(" ");
}
next = a + b;
a = b;
b = next;
}
printf("\\n");
return 0;
}
Output
0 1 1 2 3 5 8 13 21 34
How It Works
Require n >= 1 so there is at least one term to print.
Seed a = 0, b = 1.
Each iteration prints a, then computes next = a + b.
Shift: a = b, b = next — the window moves one step.
Spaces go between terms only (i < n) so there is no trailing space.
Example 2 — Recursive Single Term
Show the recurrence for one index. Do not use this to print a long series.
C
#include <stdio.h>
long long fib(int n)
{
if (n <= 0)
{
return 0;
}
if (n == 1)
{
return 1;
}
return fib(n - 1) + fib(n - 2);
}
int main(void)
{
int i;
for (i = 0; i < 7; i++)
{
printf("%lld", fib(i));
if (i < 6)
{
printf(" ");
}
}
printf("\\n");
return 0;
}
Output
0 1 1 2 3 5 8
How It Works
Base cases: fib(0) = 0, fib(1) = 1.
Otherwise return fib(n-1) + fib(n-2).
The outer loop calls fib(i) for display — fine for tiny n.
Each call tree recomputes shared subproblems, so cost grows fast.
In interviews: explain recursion, then say you would print with Example 1.
Edge Cases & Pitfalls
Check these before calling the solution done.
n = 1
Only 0
Print a single seed and stop.
Overflow
Use long long
Terms grow fast; int overflows early.
Naive recursion
Too slow for series
Do not call naive fib(i) for large counts.
Trailing space
Separate carefully
Print spaces between terms, not after the last one.
Analysis
Time and Space Complexity
Program
Time
Extra space
Iterative series
O(n)
O(1)
Naive recursive term
exponential in the index
O(n) call stack
Printing n terms needs n advances of the a/b window. That is why iteration is the default answer.
Remember
Key Takeaways
Definition: each term is the sum of the previous two.
Loop: print a, then slide a/b forward.
Recursion: good for teaching F(n), not for long series.
Test: 7 terms → 0 1 1 2 3 5 8.
One line: start 0, 1 — then repeatedly replace the pair with (b, a+b).
Frequently Asked Questions
Each term is the sum of the two before it. With seeds 0 and 1, it begins 0, 1, 1, 2, 3, 5, 8, 13, …
A simple loop is O(n) time and O(1) extra space. Naive recursion recomputes the same terms and becomes exponential.
Yes. Some texts use F1 = F2 = 1. The recurrence is the same after the first values.
Fibonacci grows quickly. Prefer long long for longer series (typical int overflows well before 50 terms).
Iterative printing is O(n). Naive recursive fib(n) for each term is much slower — avoid that for a series.