# write a program to print the Fibonacci series up to n terms

To get this series we have to add first two numbers of series to get the 3rd digit and if we want to get next digit then we have to add last two digits of series means 3rd and 4th. By using this we can get n number of Fibonacci series. The code for this series is as follows:

#include<conio.h>

void main()

{

int a=0,b=1,c,n,i;

clrscr();

cout <<"Enter value of n=";

cin>>n;

for(i=1;i<=n;i++)

{ cout <<a<<"\t";

c=a+b;

a=b;

b=c;

}

getch();

}

#include<iostream.h> used for input/output of data

#include<conio.h> provides several functions like clrscr(), getch() etc.

After that in main() function we have declared a variables a=0, b=1, c, n, I; after that we have used clrscr(); function to clear the scree when program will run. It will clear the previous output from any program. Now we have used the cout function that shows message “Enter value of n =”; and when we enter any number then this number is read by cin function and assign to n variable.

Here we have used a=0 and b=1 because we want to print 0 and 1 as the 1st and 2nd element of the series. Now we have used for loop and provide a condition i<=n means this loop will be continue till the ith value will not be equal to the entered number. Inside the loop, we have print the variable a and give a tab space using \t. after that we added the a+b and assigned to the c and after that we have assigned b to a ; and c to b. This loop will be continued till the condition will not become false.

The output is as follows: