In my program,user will be asked for 2 different options.
I chose option 2..iterative and key in any value which will then lead to the output.
However,when i choose the 1st option which is recursive,it wont output anything that its value is above 30.Meaning to say,you will see an output if key in a value of 30..& there will be no output if were to key in the value of 40 or 50.
Can anyone please test on your compiler too?Its ok if something wrong with my compiler but if there is something wrong with my code.
#include<iostream>
using namespace std;
/* Fibonacci: recursive version */
int Fibonacci_R(int n)
{
if (n <= 0) return 0;
else if (n == 1) return 1;
else return Fibonacci_R(n - 1) + Fibonacci_R(n - 2);
}
// iterative version
int Fibonacci_I(int n)
{
int fib[] = { 0, 1, 1 };
for (int i = 2; i <= n; i++)
{
fib[i % 3] = fib[(i - 1) % 3] + fib[(i - 2) % 3];
cout << "fib(" << i << ") = " << fib[i % 3] << endl;
}
return fib[n % 3];
}
int main()
{
int a, opt;
cout << "Please choose the available option:\n";
cout << "1)Recursive\n";
cout << "2)Iterative\n";
cin >> opt;
if (opt == 1)
{
cout << "Please input value:\n";
cin >> a;
Fibonacci_R(a);
cout << endl << "From recursive function" << endl;
for (int i = 1; i <= a; ++i)
cout << "fib(" << i << ") = " << Fibonacci_R(i) << endl;
cout << endl;
}
else
if (opt == 2)
{
cout << "Please input value:\n";
cin >> a;
Fibonacci_I(a);
}
system("pause");
return 0;
}