Monday, 24 September 2018

Printing Fibonacci series with n terms in C++

Fibonacci series

Fibonacci series is an infinite sequence of positive integers in which any number is the sum of two preceding (previous) numbers, except for the first two numbers. The series is as follows:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ......so on.


We can observe that each number except the first two (i.e. 0 and 1) is the sum of the previous two numbers.

Mathematically,

Printing Fibonacci series with n terms
FORMULATION OF FIBONACCI SERIES



Write a program in C++ to print Fibonacci series with n number of terms.


As the first two numbers do not satisfy the generalization, so we have to print them separately. They will not be included in the formulation. The other numbers will be printed by the formulation. The first two numbers will be initialized and printed directly as:
First number = 0
Second number = 1
Print the first number.
Print the second number.

Inside the loop, the following operation will occur:
                         Third number = First number + Second number
                         Print the third number.
                         First number = Second number
                         Second number = Third number
The for loop will run as long as the condition is true to give the required output. 


Program

#include<iostream.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,sum,i;
clrscr();
cout<<"Enter the numbers of terms you want to print=";
cin>>c;
c=c-2;
cout<<"Your fibonacci series is\n";
cout<<a<<"\n";
cout<<b<<"\n";
for(i=0;i<c;i++)
{
sum=a+b;
cout<<sum<<"\n";
a=b;
b=sum;
}
getch();
}



Output


Program to print Fibonacci series
PRINTING OF FIBONACCI SERIES WITH N TERMS













Explanation


In the program, the user wants to print 10 (value of c) terms. The first two terms (i.e. 0 and 1) are already printed, so the loop is executed (value of c-2) times. The for loop is executed as follows:

i            a            b          sum=a+b                  Output screen              a=b             b=sum
0           0             1          sum=0+1=1                 1                                     a=1              b=1
1           1             1          sum=1+1=2                 2                                     a=1              b=2
2           1             2          sum=1+2=3                 3                                     a=2              b=3
3           2             3          sum=2+3=5                 5                                     a=3              b=5
4           3             5          sum=3+5=8                 8                                     a=5              b=8
5           5             8          sum=5+8=13               13                                   a=8              b=13
6           8             13        sum=8+13=21             21                                   a=13            b=21
7           13           21        sum=13+21=34           34                                   a=21            b=34
8 (which is not less than 8, so the execution stops.)

In this way, 0 and 1 (first two terms) are printed directly and the remaining is printed by for loop as shown in the table.



No comments:

Post a Comment