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 stream and its types in c++?
What does it mean to declare a member variable as static?
daily Routine of father
Tell me can a pure virtual function have an implementation?
What is the role of C++ shorthand's?
int age=35; if(age>80) {Console.WriteLine("Boy you are old");} else {Console.WrieLine("That is a good age");}
What data encapsulation is in c++?
what is Member Functions in Classes?
Can you help me with this one? Make a program that when a user inputed a Product Name, it will display its price, and when the user inputed the quantity of the inputed product, it will show its total price. The output must be like this: Product Name: Price: Quantity: Total Price: ..this is the list of products to be inputed: Cellphone - 1500 Washing Machine - 5200 Television - 6000 Refrigirator - 8000 Oven - 2000 Computer - 11000 thanks..:D
What are disadvantages of pointers?
What is the use of dot in c++?
How do you invoke a base member function from a derived class in which you’ve overridden that function?
Explain the virtual inheritance in c++.
Does c++ vector allocate memory?
What are the benefits of c++?