Write the program for fibonacci in c++?
Answers were Sorted based on User's Feedback
Answer / nitish kumar nirala
#include<iostream.h>
#include<conio.h>
int fibo(int);
void main()
{
int n,fibbon;
cin>>n;//input number to find fabbonic series
fibbo=fibo(n);
cout<<fibbo<<"
";
getch();
}
int fibo(n)
{
if(n==0)
{
return 0;
}
elseif(n==1)
{
return 1;
}
else
{
return(fibo(n-1)+fibo(n-2));
}
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Simplest one:
#include <iostream>
using namespace std;
int main()
{
int x, a = 0, b = 1, i, z;
cout << "Lets Start!" << endl;
cout << "No. of elements in the series = ";
cin >> x;
cout << "Series: " << endl;
for(i = 0; i < x; i++){
z = a + b;
a = b;
b = z;
cout << z << endl;
}
return 0;
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / psrthipan j
#include<iostream.h>
int main()
{
int x,y;
x=y=1;
while(x<20)
{
cout<<y<<end 1;
x=x+y;
y=x-y;
}
return 0;
}
| Is This Answer Correct ? | 1 Yes | 2 No |
Answer / tanneru chetan
#include<iostream.h>
#include<process.h>
#include<conio.h>
void main()
{
clrscr();
Start:
int n,a=0,b=1,c=0,i=1;
char w;
cout<<"Enter an positive integer to represnt the
nth term of the fibonnaci series: "<<endl;
cin>>n;
cout<<"The fibonacci series upto the "<<n<<"th term
is:"<<endl;
cout<<0<<endl<<1<<endl;
while(i<=n)
{
c=a+b;
cout<<c<<endl;
a=b;
b=c;
++i;
}
cout<<"Do you want to try again(Y/N):"<<endl;
cin>>w;
if(w=='Y' || w=='y')
{
goto Start;
}
else
{
exit(4);
}
getch();
}
| Is This Answer Correct ? | 14 Yes | 17 No |
Answer / terah njehia
#include<iostream>
#include<iomanip>
using namespace std;
int fn1 = 0, fn2 = 1;
int nextfib(int* fib1, int* fib2)
{
long int next = *fib1 + *fib2;
*fib1 = *fib2;
*fib2 = next;
cout<<setw(3)<<next<<endl;
return 0;
}
int main()
{
int n;
cout<<"Enter the value n of fibonacci numbers you want to
print"<<endl;
cin>>n;
int newn = n - 2;
cout<<setw(3)<<fn1<<setw(3)<<fn2;
for (int index = 1; index <= newn; index++){
nextfib(&fn1, &fn2);
}
return 0;
}
| Is This Answer Correct ? | 11 Yes | 15 No |
Answer / abhilash
int main()
{
unsigned int i=0,j=0,sum=1,num;
printf("nEnter the limit for the series ");
scanf("%d",&num);
while(sum<num)
{
printf("%d ",sum);
i=j;
j=sum;
sum=i+j;
}
getch();
}
| Is This Answer Correct ? | 14 Yes | 27 No |
Answer / n.muthukumar
#include
int main()
{
unsigned int i=0,j=0,sum=1,num;
printf("nEnter the limit for the series ");
scanf("%d",&num);
printf("%d",i);
while(sum
{
printf("%d ",sum);
i=j;
j=sum;
sum=i+j;
}
getch();
}
| Is This Answer Correct ? | 12 Yes | 25 No |
Answer / techbots
#include<iostream>
void main()
{
int x=1,y=0;
while (x<200)
{
cout<<y<<endl;
x=x+y;
y=x-y;
}
}
| Is This Answer Correct ? | 34 Yes | 53 No |
Answer / raaz
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int range = 0;
cout<<"Enter the range of numbers\n";
cin>>range;
for(int i = 0;i<range;i++)
{
int outNum[100];
if((i==0)||(i==1))
{
outNum[i] = i;
cout<<outNum[i]<<" ";
}
else
{
outNum[i] = outNum[i-1] + outNum[i-2];
cout<<outNum[i]<<" ";
}
}
return 0;
}
| Is This Answer Correct ? | 13 Yes | 33 No |
Answer / darren chang
int fib(int input)
{
if(input==0)
return 0;
if((input==1)||(input==2))
return 1;
else
return fib(input-1)+fib(input-2);
}
| Is This Answer Correct ? | 10 Yes | 31 No |
the maximum length of a character constant can be a) 2 b) 1 c) 8
How much is size of struct having 1 char & 1 integer?
Explain deep copy?
How important is c++?
Will rust take over c++?
What do you mean by translation unit?
daily Routine of father
How many ways are there to initialize an int with a constant?
What is abstraction with real time example?
Is oops and c++ same?
Can you be able to identify between straight- through and cross- over cable wiring? And in what case do you use straight- through and cross-over?
What are the conditions that have to be met for a condition to be an invariant of the class?