Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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)

In c++ image text in transcribed
image text in transcribed
image text in transcribed
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions