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
Reverse the Linked List. Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL
Are strings mutable in c++?
What is the difference between strcpy() and strncpy()?
What are inline functions? What is the syntax for defining an inline function?
What is c++ manipulator?
Can you please explain the difference between overloading and overriding?
What are static variables?
Difference between declaration and definition of a variable.
which one is equivalent to multiplying by 2:Left shifting a number by 1 or Left shifting an unsigned int or char by 1?
What is the function to call to turn an ascii string into a long?
What is c++ in english?
What is the first name of c++?
What is stoi in c++?
What is difference between rand () and srand ()?
what you know about c++?