I have this recursive function for finding factorial, but I don't know how to modify it to throw an error when the factorial gets too big for int.
If my function was iterative, I could just put in an if-statement, checking if the factorial was 0 or less each time through, thereby finding where the overflow occurred, but this doesn't seem feasible for the recursive definition.
int factorial(int x)
{
if (x < 0) error("Can't take factorial of a negative number.");
else if (x == 0) return 1;
else return x * factorial(x - 1);
}
If for example, I call the function with 50, it will return 0. I would like to throw an error message in cases like this. Could anyone straighten me out on this?