C++ Program to Display Fibonacci Sequence

In this post, we will learn how to display Fibonacci sequences using C++ Programming language. But before that, let’s learn about the Fibonacci sequence.

The Fibonacci sequence is a series of numbers in which the next term is the sum of the previous two terms. The Fibonacci sequence starts with 0 and 1 and goes like this: 0, 1, 1, 2, 3, 5, 8, 13, . . . and so on.

Today, we will calculate the Fibonacci series using a simple for loop statement.

So, without further ado, let’s begin this tutorial.

C++ Program to Display Fibonacci Sequence

// C++ Program to Display Fibonacci Sequence
#include <iostream>
using namespace std;

int main(){
    int n, first = 0, second = 1, next_term = 0;
    
    // Asking for input
    cout << "Enter the number of terms: ";
    cin >> n;
    
    // Displaying fibonacci sequence
    cout << "Fibonacci series up to " << n << " terms are: " << endl;
    for (int i = first; i <= n; i++){
        cout << first << " ";
        next_term = first + second;
        first = second;
        second = next_term;
    }
    return 0;
}

Output

Enter the number of terms: 15
Fibonacci series up to 15 are: 
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 

How Does This Program Work ?

    int n, first = 0, second = 1, next_term = 0;

In this program, we have declared four integer data type variables named n, first, second and next_term.

    // Asking for input
    cout << "Enter the number of terms: ";
    cin >> n;

This program asks the user to enter the total number of terms. This value gets stored in the ‘n’ named variable.

    // Displaying fibonacci sequence
    cout << "Fibonacci series up to " << n << " terms are: " << endl;
    for (int i = first; i <= n; i++){
        cout << first << " ";
        next_term = first + second;
        first = second;
        second = next_term;
    }

Now, we used a for loop to calculate the Fibonacci series. Suppose, the user enters n = 5. Then, accordingly we have:

1st For Loop Iteration: for (int i = 0; i <= 5; i++), here the condition is True. So, the program will print the value of the first variable which is 0 for this case.

  • next_term = first + second = 0 + 1 = 1
  • first = second = 1
  • second = next_term = 1

Now, 2nd For Loop Iteration: for (int i = 1; i <= 5; i++), here the condition is True. So, the loop continues and prints the value of i which is 1 for this iteration.

  • next_term = first + second = 1 + 1 = 2
  • first = second = 1
  • second = next_term = 2

3rd For Loop Iteration: for (int i = 1; i <= 5; i++), here the condition is True. So, the loop will continue and print the value of i which is again 1 for this iteration. 

  • next_term = first + second = 1 + 2 = 3
  • first = second = 2
  • second = next_term = 3

4th For Loop Iteration: for (int i = 2; i <= 5; i++), here the condition is True. So, the loop will continue and print the value of i which is 2 for this iteration. 

  • next_term = first + second = 2 + 3 = 5
  • first = second = 3
  • second = next_term = 5

5th For Loop Iteration: for (int i = 3; i <= 5; i++), here the condition is True. So, the loop will continue and print the value of i which is 3 for this iteration. 

  • next_term = first + second = 3 + 5 = 8
  • first = second = 5
  • second = next_term = 8

6th For Loop Iteration: for (int i = 5; i <= 5; i++), here the condition is True. So, the loop will continue and print the value of i which is 5 for this iteration. 

  • next_term = first + second = 5 + 8 = 13
  • first = second = 8
  • second = next_term = 13

7th For Loop Iteration: (int i = 8; i <= 5; i++), here we get i = first = 8 which is greater than n. So, here the condition is False, so the loop terminates.

And we get the Sequence {0, 1, 1, 2, 3, 5}.

Conclusion

I hope after going through this post, you understand how to display a fibonacci sequence using C++ Programming language.

If you have any doubt regarding the post, then feel free to contact us in the comment section. We will be delighted to assist you.

Also Read:

1 thought on “C++ Program to Display Fibonacci Sequence”

Leave a Comment

Your email address will not be published. Required fields are marked *