Start with the iterative loop — that is the interview default for a series. Use recursion only when you need to show the mathematical recurrence for one term.
Example 1 — Iterative Series
Print the first n Fibonacci terms using two running variables.
C++
#include <iostream>
using namespace std;
void PrintFibonacci(int n)
{
if (n <= 0)
{
cout << "Enter a positive count of terms." << endl;
return;
}
long long a = 0;
long long b = 1;
for (int i = 1; i <= n; i++)
{
cout << a;
if (i < n)
{
cout << ", ";
}
long long next = a + b;
a = b;
b = next;
}
cout << endl;
}
int main()
{
PrintFibonacci(8);
return 0;
}
Output
0, 1, 1, 2, 3, 5, 8, 13
How It Works
Reject non-positive n so the loop never runs with a bad count.
Seed a = 0 and b = 1 — the first two Fibonacci numbers.
Each iteration prints a (the current term).
next = a + b builds the following term; then slide: a = b, b = next.
After n iterations you have printed exactly n terms in O(n) time.
Example 2 — Recursive nth Term
Compute a single term from the recurrence. Clear for teaching — not for printing long series.
C++
#include <iostream>
#include <stdexcept>
using namespace std;
long long Fib(int n)
{
if (n < 0)
{
throw invalid_argument("n must be non-negative.");
}
if (n == 0)
{
return 0;
}
if (n == 1)
{
return 1;
}
return Fib(n - 1) + Fib(n - 2);
}
int main()
{
cout << Fib(0) << endl; // 0
cout << Fib(1) << endl; // 1
cout << Fib(7) << endl; // 13
return 0;
}
Output
0
1
13
How It Works
Base cases: Fib(0) = 0 and Fib(1) = 1 stop the recursion.
Any larger n asks for the two previous answers and adds them.
Fib(7) expands into many overlapping calls — that is why it feels slow for large n.
Use this to explain the definition; use Example 1 to print a series in interviews.
Memoization or an iterative loop makes the same idea efficient.
Edge Cases & Pitfalls
Check these before calling the solution done.
n ≤ 0
Invalid count
Ask for a positive term count before looping.
n = 1
Only zero
Print 0 once — do not force a second term.
Overflow
Grows fast
Prefer long long for moderate n; use big integers for large ones.
Recursion
Not for series
Naive recursion is fine for demos, not for printing many terms.
Analysis
Time and Space Complexity
Program
Time
Extra space
Iterative series
O(n)
O(1)
Naive recursive nth
O(2n)
O(n) call stack
Iterative is the practical choice. Memoized recursion drops to O(n) time but still uses extra memory for the cache.
Remember
Key Takeaways
Definition: each term is the sum of the previous two (0, 1, 1, 2, 3, 5, ...).
Loop: keep a and b, print a, then slide forward.
Recursion: great for explaining F(n), poor for long series.
Test set: n=1 → 0; n=5 → 0,1,1,2,3; n=8 → …13.
One line: print a, then replace (a, b) with (b, a + b) for n steps.
Frequently Asked Questions
Each term is the sum of the previous two. Starting with 0 and 1, it goes 0, 1, 1, 2, 3, 5, 8, 13, ...
No. An iterative two-variable loop is O(n) and clear. Naive recursion repeats work and is much slower for a full series.
Yes — some textbooks do. The recurrence is the same; only the starting seeds change.
Fibonacci grows fast. int and long long eventually overflow. Cap n in beginner programs, or use a big-integer approach for large indices.
Printing n terms iteratively is O(n) time and O(1) extra space. Naive recursive Fib(n) is exponential without memoization.