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 basic structure of c++ program?
Can union be self referenced?
Write a struct time where integer m, h, s are its members?
which of the following is not an secondary constant a) array b) real c) union
What is the average salary of a c++ programmer?
Which command properly allocates memory a) char *a=new char[20]; b) char a=new char[20]; c) char a=new char(20.0);
Are vectors faster than arrays?
How we can differentiate between a pre and post increment operators during overloading?
Why is c++ considered difficult?
What is :: operator in c++?
Specify different types of decision control statements?
What are mutator methods in c++?
How can you quickly find the number of elements stored in a dynamic array? Why is it difficult to store linked list in an array?
What are the implicit member functions of class?
How many standards of c++ are there?