Question: In c++ Exception Handling Write a program to find factorial of a given number. The factorial logic is to be placed in double Factorial(int num)


Exception Handling Write a program to find factorial of a given number. The factorial logic is to be placed in double Factorial(int num) function. The factorial function should return the final computed value back to the main program. The factorial function should throw three exceptions to handle negative number(runtime error) and out of memory exception(bad alloc). Negative number exception thrown if given number is negative value and out of memory exception is thrown if the given number is greater than 20. Your program should terminate as soon a sentinel value of 999 is entered. This is where third exception of invalid argument would be raised. Hint: One try, 3 catch blocks having runtime_error, bad_alloc and invalid argument exception in main program. The factorial function should throw 3 exceptions and only when num is satisfying the three conditions (>0, !=999 and num >20), logic for calculating factorial should get executed. FACTORIAL OF A NUMBER WITH EXCEPTION CASES Enter a Number (Enter 999 to Terminate): 8 The factorial of 8 is 40320 Enter a Number (Enter 999 to Terminate): -34 Exception Occured: Math Error: Factorial of a negative cannot be found Enter a Number (Enter 999 to Terminate): 27 Exception Occured: Out of Memory Enter a Number (Enter 999 to Terminate): 999 Exception Occured: Program Terminated. Goodbye. Process exited after 22.89 seconds with return value Press any key to continue
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
