Write a Program in C++ to generate first n fibonacci terms .
Coding:-
#include<iostream.h>
int fib (int ); //function prototype
main()
{
int n,i;
cout<<"how many fibonacci terms you want ? \n";
cin>>n;
cout<<"fibonacci terms are \n";
for(i=1;i<=n;i++)
cout<<fib(i); //function calling
}
int fib (int n) //function definition
{
if (n==1)
return(0);
else
{
if (n==2)
return(1);
else
return(fib(n-1)+fib(n-2));
}
}
Output:-
Coding:-
#include<iostream.h>
int fib (int ); //function prototype
main()
{
int n,i;
cout<<"how many fibonacci terms you want ? \n";
cin>>n;
cout<<"fibonacci terms are \n";
for(i=1;i<=n;i++)
cout<<fib(i); //function calling
}
int fib (int n) //function definition
{
if (n==1)
return(0);
else
{
if (n==2)
return(1);
else
return(fib(n-1)+fib(n-2));
}
}
Output:-
No comments:
Post a Comment