Program to Print Fibonacci Series in CPP

C++C++Beginner
Practice Now

Introduction

In this lab, you will learn how to write a C++ program to generate the Fibonacci series up to a given number of terms. The Fibonacci series is a sequence of numbers in which each number is the sum of the preceding two numbers. In this lab, we will use a for loop to generate the Fibonacci series for a given number of terms entered by the user.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/conditions("`Conditions`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/ControlFlowGroup -.-> cpp/break_continue("`Break/Continue`") subgraph Lab Skills cpp/variables -.-> lab-96243{{"`Program to Print Fibonacci Series in CPP`"}} cpp/data_types -.-> lab-96243{{"`Program to Print Fibonacci Series in CPP`"}} cpp/operators -.-> lab-96243{{"`Program to Print Fibonacci Series in CPP`"}} cpp/conditions -.-> lab-96243{{"`Program to Print Fibonacci Series in CPP`"}} cpp/for_loop -.-> lab-96243{{"`Program to Print Fibonacci Series in CPP`"}} cpp/break_continue -.-> lab-96243{{"`Program to Print Fibonacci Series in CPP`"}} end

Include necessary libraries

In the main.cpp file, insert the following code to include the necessary libraries.

#include<iostream>
using namespace std;

Declare variables

Declare the necessary variables required for the program, which are n, t1, t2 and nextTerm.

int n, t1=0, t2=1, nextTerm=0;

Get the number of terms

Get the number of terms from the user and store it in the variable n.

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

Generate the Fibonacci series

Use a for loop to generate the Fibonacci series up to the given number of terms.

cout << "Fibonacci Series: ";
        for (int i=1; i <= n; ++i)
        {
            if(i == 1)
            {
                cout << " " << t1;
                continue;
            }
            if(i == 2)
            {
                cout << t2 << " ";
                continue;
            }
            nextTerm = t1 + t2;
            t1 = t2;
            t2 = nextTerm;

            cout << nextTerm << " ";
        }

Compile and run the program

To compile the program, open the terminal and navigate to the ~/project directory. Type the following command:

g++ main.cpp -o main && ./main

The program will compile and run successfully if there are no errors.

Output

Enter the number of terms in the Fibonacci series you want to generate, and the program will print out the series.
For example:

Enter the number of terms: 7
Fibonacci Series: 0 1 1 2 3 5 8

Full Code

Here's the full code for main.cpp file.

#include<iostream>
using namespace std;

int main()
{
    int n, t1=0, t2=1, nextTerm=0;

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

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

        cout << nextTerm << " ";
    }

    return 0 ;
}

Summary

In this lab, you have learned how to write a C++ program to generate the Fibonacci series up to a given number of terms. The program takes the number of terms as input from the user and prints out the Fibonacci series. You also learned how to use a for loop to generate the series and how to compile and run the program in the terminal.

Other C++ Tutorials you may like