Program for FIBONACCI SERIES in C++.

#include<iostream.h>        //Including header files

int fibnac(int n);            //function prototype for fibnac series

void main()
{
int n,i;
char ch;

do
{
//Taking the no. of terms to be found out

cout<<"How many terms of Fibonacci Series do you wanna calculate\n";
cin>>n;

for(i=1;i<=n;i++)        //Calculating the terms one-by-one and writing to the VDU
{
int p=fibnac(i);
cout<<p<<"   ";
}

cout<<endl;
cout<<"Do you wanna continue?(Y/N)\t";
cin>>ch;
cout<<endl;
}while(ch=='y'||ch=='Y');

}

int fibnac(int p)            //Function to calculate a particular fibonacci term
{
if (p==1)  return(1);
if (p==2)  return(2);
if(p>2)    return(fibnac(p-1)+fibnac(p-2));
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s