0
0
CppProgramBeginner · 2 min read

C++ Program to Print Fibonacci Series

A C++ program to print the Fibonacci series uses a loop to generate numbers where each number is the sum of the two before it, starting with 0 and 1. For example: int a=0, b=1; for(int i=0; i prints the first n Fibonacci numbers.
📋

Examples

Input5
Output0 1 1 2 3
Input1
Output0
Input0
Output
🧠

How to Think About It

To print the Fibonacci series, start with the first two numbers 0 and 1. Then, repeatedly add the last two numbers to get the next one. Keep printing each number until you reach the desired count.
📐

Algorithm

1
Get the number of Fibonacci terms to print (n).
2
Initialize two variables with 0 and 1 as the first two Fibonacci numbers.
3
Use a loop to repeat n times:
4
Print the current number.
5
Calculate the next number by adding the previous two.
6
Update the two variables to move forward in the series.
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int a = 0, b = 1;
    for (int i = 0; i < n; i++) {
        cout << a << " ";
        int next = a + b;
        a = b;
        b = next;
    }
    return 0;
}
Output
0 1 1 2 3
🔍

Dry Run

Let's trace input 5 through the code

1

Initialize variables

a = 0, b = 1

2

Iteration 1

Print 0, next = 0 + 1 = 1, a = 1, b = 1

3

Iteration 2

Print 1, next = 1 + 1 = 2, a = 1, b = 2

4

Iteration 3

Print 1, next = 1 + 2 = 3, a = 2, b = 3

5

Iteration 4

Print 2, next = 2 + 3 = 5, a = 3, b = 5

6

Iteration 5

Print 3, next = 3 + 5 = 8, a = 5, b = 8

IterationabPrinted Number
1010
2111
3121
4232
5353
💡

Why This Works

Step 1: Start with first two numbers

The Fibonacci series always begins with 0 and 1, so we set a = 0 and b = 1.

Step 2: Print current number

In each loop, we print the current number stored in a because it represents the next number in the series.

Step 3: Calculate next number

We find the next Fibonacci number by adding the last two numbers: next = a + b, then update a and b to move forward.

🔄

Alternative Approaches

Recursive approach
cpp
#include <iostream>
using namespace std;

int fib(int n) {
    if (n <= 1) return n;
    return fib(n-1) + fib(n-2);
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cout << fib(i) << " ";
    }
    return 0;
}
Simple but inefficient for large n due to repeated calculations and slow performance.
Using array to store series
cpp
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int fib[n];
    fib[0] = 0;
    if (n > 1) fib[1] = 1;
    for (int i = 2; i < n; i++) {
        fib[i] = fib[i-1] + fib[i-2];
    }
    for (int i = 0; i < n; i++) {
        cout << fib[i] << " ";
    }
    return 0;
}
Uses extra space to store all numbers, useful if you need to access series later.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs n times, each step does constant work, so total time is O(n).

Space Complexity

Only a few variables are used to store numbers, so space is O(1).

Which Approach is Fastest?

The iterative loop method is fastest and uses least memory compared to recursion or array storage.

ApproachTimeSpaceBest For
Iterative loopO(n)O(1)Fast and memory efficient
RecursiveO(2^n)O(n)Simple but slow, not for large n
Array storageO(n)O(n)When series needs to be reused or accessed randomly
💡
Use a loop with two variables to efficiently generate Fibonacci numbers without extra memory.
⚠️
Beginners often forget to update both variables correctly, causing an infinite loop or wrong output.