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
What is scope resolution operator in c++ with example?
What are smart pointers?
How many types of scopes are there in c++?
What is c++ 11 and c++ 14?
How to allocate memory dynamically for a reference?
What is purpose of abstract class?
Is java made in c++?
Can a program run without main function?
Explain mutable storage class specifier.
What is the use of class in c++?
What does obj stand for?
What does catch(…) mean?
List the types of polymorphism in c++?
What are formatting flags in ios class?
Differentiate between the message and method in c++?