Write the program for fibonacci in c++?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Do you know what are the new features that iso/ansi c++ has added to original c++ specifications?

562


Explain pass by value and pass by reference.

597


What are register variables?

650


What is rtti in c++?

629


What is a container class? What are the types of container classes in c++?

680






What is abstract class in c++?

592


Explain queue. How it can be implemented?

678


What is functions syntax in c++?

625


What is setiosflags c++?

537


Difference between inline functions and macros?

600


How do you define/declare constants in c++?

613


what is a class? Explain with an example.

621


How many keywords are used in c++?

560


What is the use of string in c++?

553


What is the auto keyword good for in c++?

626