Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; }
29th May 2018, 1:52 AM
Zeke Williams
Zeke Williams - avatar
+ 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.
29th May 2018, 1:23 AM
Zeke Williams
Zeke Williams - avatar
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 << ...
29th May 2018, 12:42 AM
Zeke Williams
Zeke Williams - avatar
0
Return -1 if input is under 0.
29th May 2018, 6:59 AM
BlazingMagpie
BlazingMagpie - avatar