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
Does dev c++ support c++ 11?
What is #include iostream?
Write about the stack unwinding?
what is C++ exceptional handling?
What is the use of setprecision in c++?
What are the advantages of using const reference arguments in a function?
Where are setjmp and longjmp used in c++?
Explain friend class?
How a new operator differs from the operator new?
Explain function overloading and operator overloading.
What is helper in c++?
What is a storage class used in c++?
Define whitespace in C++.
What is else syntax in c++?
What do you mean by vtable and vptr in c++?