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


Please Help Members By Posting Answers For Below Questions

Which sort does c++ use?

576


What things would you remember while making an interface?

561


Why is c++ still best?

547


What are the advantages of c++? Explain

590


What are the classes in c++?

636






Describe new operator and delete operator?

622


Is c++ a dying language?

581


What is the difference between object-oriented programming and procedural programming?

689


What are abstract data types in c++?

526


Explain bubble sorting.

622


What is virtual function? Explain with an example

582


Give an example of run-time polymorphism/virtual functions.

559


What is #include c++?

565


Is recursion allowed in inline functions?

605


Which bitwise operator is used to check whether a particular bit is on or off?

585