Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 2
Here is how I would do it İsmail ÖZBAKIR :
else { return -1; }
int main() {
int answer = factorial(-1);
if (answer <= -1) {
cout << "Negative number invalid";
}
else {
cout << answer << endl;
}
+ 1
You can't do that using recursion, because in C++ you can't return multiple types. You would have to throw an exception error and catch it when you call it in main.
0
You still need to return an integer. You can't use cout with a function using cout; that's like doing this:
cout << cout << ...
0
Return -1 if input is under 0.