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 the oldest programming language?
What is linked list in c++?
What does I ++ mean in c++?
Define pointers?
What do c++ programmers do?
Do you know what are static and dynamic type checking?
What is data types c++?
Is it possible to pass an object of the same class in place of object reference to the copy constructor?
Do you need a main function in c++?
What is the this pointer?
What are destructors?
What are the advantages of using pointers in a program?
What is null pointer and void pointer?
Is c++ proprietary?
What function initalizes variables in a class: a) Destructor b) Constitutor c) Constructor