Write the program for fibonacci in c++?
Answers were Sorted based on User's Feedback
Answer / kishor niit ottapalam
#include<iostream>
int main()
{
int x,y;
x=y=1;
while (x<200)
{
cout<<y<<endl;
x=x+y;
y=x-y;
}
return 0;
}
Is This Answer Correct ? | 119 Yes | 82 No |
Answer / smita
#include<iostream.h>
#include<conio.h>
class fibonacci
{
int f0,f1,f2;
public:
fibonacci()//constructor
{
f0=0;
f1=1;
f2=f0+f1;
}
void cal()
{
f0=f1;
f1=f2;
f2=f0+f1;
}
void dis()
{
cout<<"fibonacci:"<<f2<<endl;
}
};
void main()
{
fibonacci obj;
int n;
cout<<"How many no. displayed:"<<endl;
cin>>n;
for(int i=0;i<=n-1;i++)
{
obj.dis();
obj.cal();
}
getch();
}
Is This Answer Correct ? | 57 Yes | 34 No |
Answer / bharghavi
#include<iostream.h>
void main()
{
int a=-1;b=1,c;
for(int i=0;i<200;i++)
{
c=a+b;
cout<<c<<"\t";
a=b;
b=c;
}
Is This Answer Correct ? | 83 Yes | 65 No |
Answer / jitendra goyal
#include<stdio.h>
#include<conio.h>
main()
{
int a=0,b=1,n,c;
printf("how many element you want in series");
scanf("%d",&n);
printf("%d%d",a,b);
for(i=0;i<=n-2;i++)
{
c=a+b;
print("%d",c);
a=b;
b=c;
}
getch();
}
Is This Answer Correct ? | 5 Yes | 1 No |
Answer / gobinath
#include<iostream>
using namespace std;
class A
{
public:
void print()
{
int a=0,b=1,c=0,n;
cout<<"Enter the number of : "<<endl;
cin>>n;
cout<<a<<" "<<b<<" ";
for(int i=1;i<=n-2;i++)
{
c=a+b;
a=b;
b=c;
cout<<c<<" ";
}
}
};
int main()
{
A a;
a.print();
}
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / ria
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr ();
int n,f1,f2,f3,i;
cout<<"/n input the no.of terms";
cin>>n;
f1=-1;
f2=1;
cout<<"/n the fibonacci series is ";
for(i=1,i<=n,i++)
{
f3=f1+f2;
cout<<"/n"<<f3;
f1=f2;
f2=f3;
}
getch();
}
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / irushad abdulrahman
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a=-1, b=1, c, x;
cout << "Enter a number to find FIBONACCI less than
that:";
cin >>x;
do
{
c=a+b;
cout << c << "\t";
a=b;
b=c;
}
while (c<=(x-c));
cout <<"\n";
system ("pause");
return 0;
}
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / srinivasan
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=0,b=1,c=0,n;
cout<<"Enter the number: ";
cin>>n;
cout<<a<<" "<<b<<" ";
for(int i=1;i<=n;i++)
{
c=a+b;
a=b;
b=c;
cout<<c<<" ";
}
getch();
}
Is This Answer Correct ? | 3 Yes | 2 No |
Answer / joy
#include<iostream.h>
#include<conio.h>
voidmain()
{
clrscr();
int f1=0,f2=1,f3,n,i;
cout<<"Enter the number of terms...";
cin>>n;
cout<<"Fibonacci Series\n";
cout<<f1<<'\n'<<f2<<'\n';
for(i=3;i<=n;i++)
{
f3=f1+f2;
cout<<f3<<'\n';
f1=f2;
f2=f3;
}
getch();
}
Is This Answer Correct ? | 3 Yes | 2 No |
Answer / gobicsk
#include <iostream>
using namespace std;
const int n = 20;
long result[n];
int fibonacci( int m )
{
if( result[m] > 0 )
// We already computed it.
return result[m];
int answer;
if( m == 0 )
answer = 0;
else
if( m == 1 )
answer = 1;
else
answer = fibonacci( m - 1 ) + fibonacci( m - 2 );
// Save answer for re-use.
result[m] = answer;
return answer;
}
int main()
{
fibonacci( n );
cout << "\n Fibonacci Series \n";
for( int i = 0; i <= n; i++ )
cout << "\n Fibonacci(" << i << ") = " << result[i];
}
Is This Answer Correct ? | 1 Yes | 0 No |
Which operations are permitted on pointers?
What is a loop? What are different types of loops in c++?
What are the advantages of using friend classes?
Can the creation of operator** is allowed to perform the to-the-power-of operations?
What is the difference between containment and delegation?
What are the steps in the development cycle?
How the compilers arranges the various sections in the executable image?
What will the line of code below print out and why?
Can you declare an array without a size in c++?
What is pointer -to-members in C++? Give their syntax?
What is the difference between a declaration and a definition?
What is the need of a destructor?