# C++ program to generate Fibonacci series using while loop

Last updated:27th Aug 2022

In this example, you will write a c++ program for generating the Fibonacci series by using a while loop.

**Example:**

Fibonacci series of 10 terms.

input: 10 output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Fibonacci series is the series where the next number is the addition of the previous two numbers.

```
#include <iostream>
using namespace std;
int main() {
int a = 0, b = 1, term = 0, n;
cout << "Enter the length of Fibonacci Series: ";
cin >> n;
// displays the first two terms i.e 0 and 1
cout << "Fibonacci Series: " << a << ", " << b << ", ";
term = a + b;
/* n!=2 because we have already printed the first 2 terms. */
while(n!=2) {
cout << term << ", ";
a = b;
b = term;
term = a + b;
n--;
}
return 0;
}
```

**output**

Enter the length of fibonacci Series: 10 Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

**Working:**

In the above program, take the length of a series from a user and store it in variable `n`

.

Fibonacci series starts with 0 and 1 so print the first two-term.

Since we have already printed the first two-term, now we will iterate the while loop until `n!=2`

. Inside while loop generates all other remaining terms.