# Fibonacci Series in C++ using Do-While Loop

Before going to the program first let us understand what is a *Fibonacci Series?*

**Fibonacci Series:**

Fibonacci series is nothing but a Series of Numbers which are found by adding the two preceding(previous) Numbers.

For Example,

0,1,1,2,3,5,8,13,21 is a Fibonacci Series of length 9.

For more understanding you can check out the following link:

https://www.mathsisfun.com/numbers/fibonacci-sequence.html

## Program code to Display Fibonacci Series in C++:

#include<iostream.h> #include<conio.h> void main() { int n,f,f1=-1,f2=1; clrscr(); cout<<" Enter The Number Of Terms:"; cin>>n; cout<<" The Fibonacci Series is:"; do { f=f1+f2; f1=f2; f2=f; cout<<" \n"<<f; n--; }while(n>0); getch(); }

**Related: **Fibonacci Series in C using Do-While Loop

## Working:

- First the computer reads the value of number of terms for the Fibonacci series from the user.
- Then using do-while loop the two preceding numbers are added and printed.
- The loop continues till the value of number of terms.

## Step by Step working of the above Program Code:

Let us assume that the Number of Terms entered by the user is 5.

- It assigns the value of n=5.
- Then the loop continues till the condition of the do-while loop is true.

2.1. do

f=f1+f2 (f=-1+1) So f=0

f1=f2 (f1=1) So f1=1

f2=f (f2=0) So f2=0

print the value of “f” (ie. 0)

n – – (n=n-1) So n=4

n>0 (4>0), do-while loop condition is true

2.2. do

f=f1+f2 (f=1+0) So f=1

f1=f2 (f1=0) So f1=0

f2=f (f2=1) So f2=1

print the value of “f” (ie. 1)

n – – (n=n-1) So n=3

n>0 (3>0), do-while loop condition is true

2.3. do

f=f1+f2 (f=0+1) So f=1

f1=f2 (f1=1) So f1=1

f2=f (f2=1) So f2=1

print the value of “f” (ie. 1)

n – – (n=n-1) So n=2

n>0 (2>0), do-while loop condition is true

2.4. do

f=f1+f2 (f=1+1) So f=2

f1=f2 (f1=1) So f1=1

f2=f (f2=2) So f2=2

print the value of “f” (ie. 2)

n – – (n=n-1) So n=1

n>0 (1>0), do-while loop condition is true

2.5. do

f=f1+f2 (f=1+2) So f=3

f1=f2 (f1=2) So f1=2

f2=f (f2=3) So f2=3

print the value of “f” (ie. 3)

n – – (n=n-1) So n=0

n>0 (0>0), do-while loop condition is false

2.6. It comes out of the do-while loop.

- Thus the program execution is completed.

## Output:

### TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

luv this hats off

Thanks Bunny 🙂