# Fibonacci series C++ Program

Written by

**Fibonacci series program in C++**

**Fibonacci series** is a series in which the next term is the sum of the previous two numbers. Here, we’ll write a program to print the Fibonacci series on the basis of series elements to be printed.

`Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21`

**Logic:**

We will take two variables with values 0 and 1. Then a third var which will be the sum of the first two var and then loop through it.

**Algorithm:**

- Take the input for the series of elements to be printed.
- Take two variables, pre and next and assign pre = 0 and next = 1.
- Take another variable, last which will be the sum of pre and next.
- Run a while loop.
- Print the value of pre.
- Change the values of pre, next and last in the loop.
- End loop after n iterations.

**Code:**

```
#include<iostream>
using namespace std;
int main()
{
int n,pre,next,last;
cout<<"How many numbers of fibonacci series do you want to print?";
cin>>n;
pre=0; //previous number
next=1; //next number
last=pre+next;
while(n>0)
{
cout<<"\n"<<pre;
pre=next; //pushing the three values ahead
next=last;
last=pre+next; //third number is sum of new first and second number
n--;
}
return 0;
}
```

**Output:**

```
How many numbers of Fibonacci series do you want to print? 10
0
1
1
2
3
5
8
13
21
34
```