C++ Fibonacci Series Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is the Fibonacci Series?

The Fibonacci series starts with 0 and 1. Every next term is the sum of the previous two.

Remember
F(0) = 0
F(1) = 1
F(n) = F(n - 1) + F(n - 2)   for n >= 2

First terms:
  0, 1, 1, 2, 3, 5, 8, 13, 21, ...
n (terms)Series
10
50, 1, 1, 2, 3
80, 1, 1, 2, 3, 5, 8, 13

How to Print a Fibonacci Series in C++

Four clear steps — then pick an iterative loop for the series or recursion for a single term.

  1. Start with seeds a = 0 and b = 1.
  2. Print (or store) the current term.
  3. Set next = a + b, then shift: a = b, b = next.
  4. Repeat until you have printed n terms.
MethodIdeaTimeBest for
Iterative loopKeep two running termsO(n)Printing a series (default choice)
Recursive nth termFib(n) = Fib(n-1) + Fib(n-2)Exponential (naive)Teaching the recurrence only

Pseudocode

Pseudocode
function PrintFibonacci(n):
    if n <= 0: return

    a = 0
    b = 1

    for i from 1 to n:
        print a
        next = a + b
        a = b
        b = next

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Seeds0, then 1 (common interview choice)
Updatenext = a + b; then slide the window
n = 1Print only 0
n = 2Print 0 1
Series vs nthLoop for many terms; recursion only for demos
OverflowCap n or use long long / big integers

Live Preview

Enter how many terms to print and watch the series grow term by term.

Positive integers from 1 to 40. Tap a chip or type a value.

Result
n = 8
Series: 0, 1, 1, 2, 3, 5, 8, 13

Worked Walkthrough — n = 8

Keep two variables and slide them forward after each print.

Calculation
Start: a = 0, b = 1

Print 0 → next = 0+1 = 1 → a=1, b=1
Print 1 → next = 1+1 = 2 → a=1, b=2
Print 1 → next = 1+2 = 3 → a=2, b=3
Print 2 → next = 2+3 = 5 → a=3, b=5
Print 3 → next = 3+5 = 8 → a=5, b=8
Print 5 → next = 5+8 = 13 → a=8, b=13
Print 8 → next = 8+13 = 21 → a=13, b=21
Print 13

Series: 0, 1, 1, 2, 3, 5, 8, 13

C++ Programs

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;
}

How It Works

  1. Reject non-positive n so the loop never runs with a bad count.
  2. Seed a = 0 and b = 1 — the first two Fibonacci numbers.
  3. Each iteration prints a (the current term).
  4. next = a + b builds the following term; then slide: a = b, b = next.
  5. 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;
}

How It Works

  1. Base cases: Fib(0) = 0 and Fib(1) = 1 stop the recursion.
  2. Any larger n asks for the two previous answers and adds them.
  3. Fib(7) expands into many overlapping calls — that is why it feels slow for large n.
  4. Use this to explain the definition; use Example 1 to print a series in interviews.
  5. 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.

Time and Space Complexity

ProgramTimeExtra space
Iterative seriesO(n)O(1)
Naive recursive nthO(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.

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.

Next: GCD

Find the greatest common divisor of two numbers.

GCD tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful