C++ Program to generate Fibonacci series up to n number of terms

In this C++ program, we have to generate the Fibonacci series up to n number of terms.

What is a Fibonacci series?

The Fibonacci sequence is a series of integers in which every number after the first two numbers is the sum of the two preceding numbers. The first two numbers are set to 0 and 1 manually. All the rest of the numbers in the series is obtained by calculating the sum of the previous two numbers.
Example:    0, 1, 1, 2, 3, 5, 8, 13, 21, ………..

How can we generate the Fibonacci series up to n number of terms?

Since each term except the first two terms is obtained by adding the previous two terms, we can generate a rule to find the nth term in the series.

Xn = X(n-1) + X(n-2) ,Where:

  • Xn is the nth term
  • X(n-1) is the (n-1)th term
  • X(n-2) is the (n-2)th term.

X(n-1) and  X(n-2) are the previous two terms of the nth term.
For Example: Consider the Fibonacci series 
n    =    0    1    2    3    4    5    6    7      8    9
Xn  =    0    1    1    2    3    5    8   13    21    

The 9nth term is calculated like this:
X9 = X(9-1) + X(9-2)
     = X8+ X7
     = 21 + 13
     = 34.

C++ Program to generate Fibonacci series up to n number of terms

How can we implement a C++ program to generate Fibonacci series up to n terms?

Start the program with the main function. Declare an integer type variable n to hold the value entered for the number of terms. Set the first two terms X1  and X2 to 0 and 1. X1 = 0 and X2 = 1. And another integer variable nextterm = 0. Ask the user to enter the number of terms. Copy the entered value to n. Print the first term X1 and the second term X2.Then print nextterm by adding X1 and X2.After that assign X1 = X2; X2 = nextterm;This process will continue until the number of terms reaches the n

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main().

Step 4: Declare integer type variable n, X1 = 0, X2 = 1, nextterm=0.

Step 5: Print a message “Enter the number of terms”, and read the character to the variable n.

Step 6: Print the first two terms X1 and X2

Step7: Generate the Fibonacci series using for loop

Step8: Calculate the nexttern by adding the previous two terms; nextterm = X1 +X2

Step9: Print the nexttern of the series.

Step 10: Update the terms X1 = X2 and X2 = nextterm

Step 11: The process continue until the number of terms reaches the n

Step 12: Exit.
 

C++ Source Code

                                          #include <iostream>
using namespace std;

int main() {
    int n, X1 = 0, X2 = 1, nextterm = 0;

    cout << "Enter the number of terms: ";
    cin >> n;

    cout << "Fibonacci Series: ";
    
    // Prints the first two terms.
    cout << X1 << ", ";
    cout << X2 ;
    
    for (int i = 1; i < n; ++i) {
      
        nextterm = X1 +X2;
        X1 = X2;
        X2 = nextterm;
        
        cout << ", " << nextterm ;
    }
    return 0;
}
                                      

OUTPUT

Enter the number of terms: 6
Fibonacci Series: 0, 1, 1, 2, 3, 5,