# C++ Program to Display Fibonacci Sequence

In this post, you will learn how to Display Fibonacci Sequence using C++ programming language.

This lesson will teach you how to Display Fibonacci Sequence, with a for loop, assign and substitute method and a mathematical function using the C++ Language. Let’s look at the below source code.

## Source Code

```#include <iostream>
using namespace std;
int main()
{
int t1 = 0, t2 = 1, nextTerm = 0, n;
cin >> n;
cout << "Enter a positive number: "<< n<<endl;
cout << "\nFibonacci Series: " << t1 << ", " << t2 << ", ";
nextTerm = t1 + t2;
while(nextTerm <= n)
{
cout << nextTerm << ", ";
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}```

`10`

## Output

```Enter a positive number: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8,```

#include <iostream>

• This line which is called the header file. `#include` statement tells the compiler to use available files and `<iostream>`is the name of the specific that we have used in this code. The `<iostream>` file stands for Input and Output statement.

using namespace std;

• The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.

int main();

• This line usually controls the function of the code, as it calls the functions to perform their tasks.
• The `int main()`shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.

{ }

• The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
• Every statement and value between these braces belong to the main function.

The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.

1. In the Fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1.
2. First we initialize the variables t1, t2, nextTerm, n as integers and assign t1=0 and t2=1. We then collect the number from the user and store it in the variable n, using function `cin>>`  and display the value using `cout<<` and the Insertion Operators'<<‘ , ‘>>’.
3. The Fibonacci sequence is limited to the value of n, as the end of the range
4. Next display the value of t1 and t2 with a comma to separate the displayed values `cout << "\nFibonacci Series: " << t1 << ", " << t2 << ", ";`
5. Using the addition operator we add the values t1 and t2 and store it in the variable nextTerm `nextTerm = t1 + t2;`
6. This is followed by a while loop. A while loop executes the block statement continuously until the condition is satisfied. The condition of the while loop is `while(nextTerm <= n)`
7. The block statement of the loop has functions which uses the assigning and substitution method. We us this method as the numbers increase as the execution moves forward
8. For example the initial values of t1 and t2 are 0 and 1 respectively when the block is executed t1 becomes 1 and t2 becomes the calculated value and these new values are used in the function `nextTerm = t1 + t2;` .
9. Then the value of nextTerm  is used in the verify if the condition is true. When true the while loop is executed multiple times till the condition is not satisfied.
10. When the condition is not satisfied, the execution is terminated and the output is displayed.

Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line and ‘\n’ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.

You can also try the source code given below. The following code is structured using a for loop, a mathematical function, if statements and additional statement which asks for a specific range. Let’s look at the below source code.

```#include <iostream>
using namespace std;
int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;
cin >> n;
cout << "Enter the number of terms: "<< n <<endl;
cout << "\nFibonacci 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;
}```

`10`

## Output

```Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,```
1. In the above source code, we first declare the variables to be used as integers and assign their values.
2. Next we collect the number of elements the Fibonacci sequence should contain and store it in the variable n.
3. We declare a condition for the for loop, and for the if statements. When the code is executed, the for loop condition is verified. When the condition is true, the for loop statement (if statements) is executed.
4. The assigning and substitution method used is similar to the previous code.
5. The for loop is executed multiple times till the condition is not satisfied, when the condition becomes false the execution is terminated and the Fibonacci Sequence is displayed.