int main()
{
int x;
cout << "Enter a number greater than or equal to 0: ";
cin >> x;
if (x>=0)
{
f(x);
}
while (x<0)
{
cout << "Please enter a valid number!\n";
cout << "Enter a number greater than or equal to 0: ";
cin >> x;
}
}
int f(int n)
{
if (n==0)
{
return f(n)=5; //base case
}
else
{
f(n) = 2*f(n-1)-3; //recursive case
}
}
But the objective is that the user enters a number suppose the number 8. The recursive function has to run from f(0) to f(8), and the base case that I was given is that f(0) = 5. And that is supposed to be used to find f(1) like this f(1) = 2*f(1-1)-3 which simplifies to f(1) = 2*f(0)-3.
#include <iostream>
int f(int n)
{
if (n == 0)
return 5; // base case
elsereturn 2 * f(n-1) - 3; // recursive case
}
int main()
{
int x = 0;
while (true)
{
std::cout << "Enter a number greater than or equal to 0: ";
std::cin >> x;
if (x >= 0) break;
std::cout << "Please enter a valid number!\n";
}
std::cout << f(x) << '\n';
}