Write the program for fibonacci in c++?
Answer Posted / 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 |
Post New Answer View All Answers
Which sort does c++ use?
What things would you remember while making an interface?
Why is c++ still best?
What are the advantages of c++? Explain
What are the classes in c++?
Describe new operator and delete operator?
Is c++ a dying language?
What is the difference between object-oriented programming and procedural programming?
What are abstract data types in c++?
Explain bubble sorting.
What is virtual function? Explain with an example
Give an example of run-time polymorphism/virtual functions.
What is #include c++?
Is recursion allowed in inline functions?
Which bitwise operator is used to check whether a particular bit is on or off?