The recursive fibonacci function. : function recursion « Function « C++ Tutorial






#include <iostream>
using std::cout;
using std::cin;
using std::endl;

unsigned long fibonacci( unsigned long );

int main()
{
   cout << "fibonacci( 20 ) = " << fibonacci( 20 ) << endl;
   cout << "fibonacci( 30 ) = " << fibonacci( 30 ) << endl;
   cout << "fibonacci( 35 ) = " << fibonacci( 35 ) << endl;
   return 0;
} 

unsigned long fibonacci( unsigned long number )
{
   if ( ( number == 0 ) || ( number == 1 ) )
      return number;
   else 
      return fibonacci( number - 1 ) + fibonacci( number - 2 );
}
fibonacci( 20 ) = 6765
fibonacci( 30 ) = 832040
fibonacci( 35 ) = 9227465








7.11.function recursion
7.11.1.Demonstrate recursion
7.11.2.Print a string backwards using recursion
7.11.3.Recursive factorial function
7.11.4.The recursive fibonacci function.
7.11.5.The iterative factorial method.