# Program to display Fibonacci Series using C++

Mathematics has a lot of wonders in it. There are many equations and series which can define the various phenomena of nature. There are many equations that are beautiful when graphically represented. One of such series is Fibonacci series.

#include<iostream.h>
#include<conio.h>

The header files used for the program are <iostream.h> and <conio.h> for input output operations.

## The main() program

Let’s divide the program in two parts for better understanding.

void main()
{
clrscr();
int f[1000],i,n,a;
f[0]=0;
f[1]=1;
cout<<"Enter the required number of terms";
cin>>n;

Starting with the main() function, we first use clrscr() function to clear the console screen.

Then integer variables ‘i’, ‘n’ and ‘a’ are initialized. An array ‘f’ of maximum value ‘1000’ is also initialized. The length of the array can be anything, it depends upon the length of the Fibonacci series required by the user. Then the first two elements of the array ‘f’ initialized in the previous line need to be defined. The value of the first and second elements of the array are valued as 0 and 1 respectively. This is the standard declaration required for the Fibonacci Series.

Then, a statement is printed for the user to enter the number of terms of Fibonacci Series required to be printed. The input value is then stored in the variable ‘n’.

for(i=0;i<n;i++)
{
f[i+2]=f[i]+f[i+1];
cout<<f[i+2]<<"\n";
}
getch();
}

In the second half, a ‘for’ loop is used. The ‘for’ loop for ‘i’ starts from 0 and runs till value ‘n’ which is a user-entered value. In ‘for’ loop, the sum of the already defined elements of array f[0] and f[1] is equated to the third element of the array. From this line, the third element of the array gets a value equal to the sum of the previous two elements of the array. The next line prints the element whose value is newly assigned, and the ‘for’ loop is ended.

getch() is used to hold the console screen till all the input output operations take place.

#### Output

Enter the required number of terms
5
1
2
3
5
8