Below is my code for Fibonacci sequence, however when I use a large number such as 100 or more it takes forever to produce output. Why? any help would be appreciated. :(
#include <iostream>
using namespace std;
long fib_num ( long );
int main()
{
long num = 0;
long sequence = 0;
cout << " Enter a positive number and I will compute the Fibonacci sequence for that number: ";
cin >> num;
if ( num < 0)
{ cout << " Number must be greater than zero " << endl;
return 1;
}
cout << endl << endl;
sequence = fib_num(num);
cout << " The " << num << "th number of the Fibonacci sequence is " << sequence << endl;
return 0;
}
long fib_num ( long n)
{
if ( n == 0)
{
return 0;
}
else if ( n == 1 )
{
return 1;
}
else
return fib_num( n -1) + fib_num(n-2);
}
<< moderator edit: added code tags: [code][/code] >>