Fibonacci Series in C++ using 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,34,55 is a Fibonacci Series of length 11.

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:";
    
    while(n>0)
    {
        f=f1+f2;
        f1=f2;
        f2=f;
        cout<<"  \n"<<f;
        n--;
    }
    getch();
}

Related: Fibonacci Series in C using While Loop

Working:

  • First the computer reads the value of number of terms for the Fibonacci series from the user.
  • Then using 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.

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

2.1.   n>0      (5>0), while loop condition is true

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

2.2.   n>0      (4>0), while loop condition is true

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

2.3.   n>0      (3>0), while loop condition is true

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

2.4.   n>0      (2>0), while loop condition is true

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

2.5.   n>0      (1>0), while loop condition is true

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

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

comes out of the while loop.

  1. Thus the program execution is completed.

Output:

fibonacci series
fibonacci series

TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *