C++ Program For Fibonacci Numbers

Last Updated : 18 Aug, 2026

The Fibonacci series is a sequence in which each number is the sum of the previous two numbers. It starts with 0 and 1, and the remaining terms are generated using this recurrence relation:

  • F(0) = 0 and F(1) = 1.
  • Each subsequent term is calculated as the sum of the previous two terms.

Illustration

Input: 5
Output: 5

Explanation: In the Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13..., the value at index 5 is 5 (using 0-based indexing).

Methods to Find the Nth Fibonacci Number in C++

The nth Fibonacci number can be calculated using different approaches. Each method has different time and space requirements.

1. Using Recursion

The recursive approach directly follows the Fibonacci recurrence relation. The function calls itself to calculate the two preceding Fibonacci numbers.

Approach

  • If n is 0 or 1, return n.
  • Otherwise, recursively calculate F(n - 1) and F(n - 2).
  • Return their sum.
C++
#include <bits/stdc++.h>
using namespace std;

int fib(int n) {

	// If n is 1 or 0, then return n, 
  	// works for 0th and 1st terms
    if (n <= 1)
        return n;

    // Recurrence relation to find 
  	// the rest of the terms
    return fib(n - 1) + fib(n - 2);
}

int main() {
    int n = 5;
  
  	// Finding nth term
    cout << fib(n);
    return 0;
}

Output
5

Explanation: The function keeps making recursive calls until it reaches the base cases F(0) and F(1). The returned values are then added to obtain the required Fibonacci number.

2. Optimized Recursive Approach

The basic recursive approach repeatedly calculates the same Fibonacci values. This can be avoided by passing the previous two Fibonacci numbers as parameters during recursive calls.

Approach

  • Maintain the previous two Fibonacci values.
  • Pass them to the next recursive call.
  • Update both values at every step.
C++
#include <iostream>
using namespace std;

// Helper function to find the 
// nth fibonacci number using recursion
int fibHelper(int n, int prev2, int prev1) {
  
    // When n reaches 0, return prev2
    if (n == 0) {
        return prev2;
    }
  
    // When n reaches 1, return prev1
    if (n == 1) {
        return prev1;
    }
    
    // Recursive call with updated 
    // parameters to find rest 
  	// of the fibonacci numbers
    return fibHelper(n - 1, prev1, prev2 + prev1);
}

int fib(int n) {

  	// Calling recursive function
    return fibHelper(n, 0, 1);
}

int main() {
    int n = 5;

    // Finding the nth Fibonacci number
    cout << fib(n);
    return 0;
}

Output
5

Explanation: Instead of recalculating previous Fibonacci numbers, the function carries forward the last two values in each recursive call.

3. Using Iteration

The iterative approach generates Fibonacci numbers sequentially using a loop. It is generally preferred when only the nth Fibonacci number is required because it avoids recursion and uses constant extra space.

Approach

  • Initialize the first two Fibonacci numbers as 0 and 1.
  • Generate each subsequent term by adding the previous two terms.
  • Continue until the nth term is reached.
  • Return the current term.
C++
#include <bits/stdc++.h>
using namespace std;

int fib(int n) {
  
  	// For 0th and 1st term
    if (n <= 1)
        return n;
	
  	// Variable to store the 
  	// last two terms
    int prev1 = 1, prev2 = 0;
  	
  	// Variable that stores the 
  	// current fibonacci term
  	int curr;

    // Calculating the next 
    // fibonacci  number by using
  	// the previous two number
    for (int i = 2; i <= n; i++) {
        curr = prev1 + prev2;
        prev2 = prev1;
        prev1 = curr;
    }
    return curr;
}

int main() {
    int n = 5;
    cout << fib(n);
    return 0;
}

Output
5

Explanation: The loop continuously updates the last two Fibonacci numbers until the required term is found.

4. Using Matrix Exponentiation

Matrix exponentiation uses the Fibonacci transformation matrix to calculate the nth Fibonacci number in logarithmic time. It applies divide-and-conquer to efficiently compute the required matrix power.

The transformation matrix is:

\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}

Raising this matrix to the power n - 1 gives the nth Fibonacci number at the top-left position.

C++
#include <iostream>
using namespace std;

void multiply(long long F[2][2], long long M[2][2]) {
    long long a = F[0][0] * M[0][0] +
                  F[0][1] * M[1][0];

    long long b = F[0][0] * M[0][1] +
                  F[0][1] * M[1][1];

    long long c = F[1][0] * M[0][0] +
                  F[1][1] * M[1][0];

    long long d = F[1][0] * M[0][1] +
                  F[1][1] * M[1][1];

    F[0][0] = a;
    F[0][1] = b;
    F[1][0] = c;
    F[1][1] = d;
}

void power(long long F[2][2], int n) {
    if (n == 0 || n == 1)
        return;

    long long M[2][2] = {
        {1, 1},
        {1, 0}
    };

    power(F, n / 2);
    multiply(F, F);

    if (n % 2 != 0)
        multiply(F, M);
}

long long fib(int n) {
    if (n <= 1)
        return n;

    long long F[2][2] = {
        {1, 1},
        {1, 0}
    };

    power(F, n - 1);

    return F[0][0];
}

int main() {
    int n = 5;

    cout << fib(n);

    return 0;
}
Try It Yourself
redirect icon

Output
5

Explanation: The transformation matrix contains the Fibonacci recurrence. By repeatedly squaring the matrix, its power can be calculated efficiently without computing every preceding Fibonacci number individually.

Comment