C++ Program to Display Fibonacci Series using Loops, Recursion, Dynamic Programming

In the previous article, we have discussed about C++ Program to Reverse a Number. Let us learn how to Display Fibonacci Series in C++ Program. Know different methods to print Fibonacci series in C++ explained step by step with sample programs. You can use the technique of your choice to display the Fibonacci series in no time. You can use this quick tutorial over here as a reference to resolve any doubts of yours.

Methods to display Fibonacci series in c++

In this article, we understand how to display the Fibonacci series in c++. Methods that we will discuss are given below.

Before understanding these methods let us understand first what is Fibonacci series is. The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..

Here we can see the next number in the series is the sum of the previous two numbers. Now we see different methods to display the series.

Method 1-Using Loops

In this method, we are given a number up to which we have to print the series. As we understand that in this series the next number is the sum of previous two numbers therefore we have to store first two number in a variable by ourself because these numbers are fixed and by these numbers, we can display the series. Let write the code for this.

C++ Program to print Fibonacci Series using Loops

#include <iostream>
using namespace std;

int main() {
    int n=10, t1 = 0, t2 = 1, nextTerm;
    cout << "Fibonacci Series: ";

    for (int i = 0; i < n-2; i++) {
       
        if(i == 0) {
            cout << t1 << " ";
        }
        if(i == 1) {
            cout << t2 << " ";
        }
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
        
        cout << nextTerm << " ";
    }
    return 0;
}

Output

Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

Method 2: Using Recursion

As we understand that in this series the next number is the sum of the previous two numbers, therefore, we have to build faith in our program that the sum of the last two-digit will give the next digit, and by having this faith we call the recursive function. Let write code for this.

C++ Program to print Fibonacci Series using Recursion

#include <iostream>
using namespace std;

int fibonacci(int n){
    
    if(n==1){
        return 0;
    }
    if(n==2){
        return 1;
    }
    return fibonacci(n-1)+fibonacci(n-2);
}
int main() {
    int n=10, t1 = 0, t2 = 1, nextTerm;
    for(int i=1;i<=n;i++)
    {
     cout<<fibonacci(i)<<" ";
    }
    return 0;
}

Output

0 1 1 2 3 5 8 13 21 34

Method – 3 Using Dynamic Programming

When we analyze the recursive code of the Fibonacci series we will see that we call the recursive function for a number multiple times which is not a good practice. In this approach, we will see that instead of calling recursive function for a given number again and again we store the result for that number in an array so that if that number come again instead of calling recursive call for that number we will simply return the result of that number from the array. Let write the code for this.

#include <iostream>
using namespace std;

int fibonacci(int n,int f[]){
    
    if(n==1){
        return 0;
    }
    if(n==2){
        return 1;
    }
    if(f[n] != -1){
        return f[n];
    }
    
    f[n] = fibonacci(n-1,f)+fibonacci(n-2,f);
    return f[n];
}
int main() {
    int n=10, t1 = 0, t2 = 1, nextTerm,f[10];
    for(int i=0;i<=n;i++)
    {
        f[i]=-1;
    }
    for(int i=1;i<=n;i++)
    {
     cout<<fibonacci(i,f)<<" ";
    }
    return 0;
}

Output

0 1 1 2 3 5 8 13 21 34

So these are the methods to display the Fibonacci series in c++.

Related Programs: