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 |
Eplain extern keyword?
Differentiate between an external iterator and an internal iterator?
What is the difference between set and map in c++?
What is a catch statement?
Write a Program for dynamically intialize a 2 dimentional array. Eg:5x20, accept strings and check for vowels and display the no.finally free the space allocated .
Explain what are the sizes and ranges of the basic c++ data types?
In a class, there is a reference or pointer of an object of another class embedded, and the memory is either allocated or assigned to the new object created for this class. In the constructor, parameters are passed to initialize the data members and the embedded object reference to get inialized. What measures or design change should be advised for proper destruction and avioding memory leaks, getting pointers dangling for the embedded object memory allocation? Please suggest.
What is the difference between public and private data members?
what is the difference between linear list linked representaion and linked representation? what is the purpose of representing the linear list in linked represention ? is it not avoiding rules of linear represention?
Why is that unsafe to deal locate the memory using free( ) if it has been allocated using new?
What is operators in c++?
Explain rtti.