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


Please Help Members By Posting Answers For Below Questions

Reverse the Linked List. Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL

548


Are strings mutable in c++?

884


What is the difference between strcpy() and strncpy()?

834


What are inline functions? What is the syntax for defining an inline function?

786


What is c++ manipulator?

715


Can you please explain the difference between overloading and overriding?

805


What are static variables?

800


Difference between declaration and definition of a variable.

862


which one is equivalent to multiplying by 2:Left shifting a number by 1 or Left shifting an unsigned int or char by 1?

945


What is the function to call to turn an ascii string into a long?

809


What is c++ in english?

802


What is the first name of c++?

758


What is stoi in c++?

907


What is difference between rand () and srand ()?

812


what you know about c++?

848