Starting from:
$35

$29

CS1811 Excep&onal Division Solved


Purpose

To become familiar with catching excep4ons.



Direc&ons:

Your task is to write a program that divides two integers. The program should:

Prompt the user for the numerator and read it in. The value must be an integer. If it is not, catch the associated excep4on and display the message “Error: you must enter a whole number” to the user and re-prompt the user for a numerator.

Do the same thing for the denominator.

Display the message <numerator> / <denominator> = <answer> to the user. If the user aJempts to divide by zero, display the message “Error: the denominator can’t be zero”, re-prompt the user for a new denominator, and try again to display the answer message.

HINT: If reading in a number from the user results in an excep&on, you will likely need to read in the new line (i.e. “enter”) character following the number prior to reading in any more numbers.


Example output:

What is the numerator? bob

Error: you must enter a whole number What is the numerator? 2

What is the denominator? fred

Error: you must enter a whole number What is the denominator? 0

Error: the denominator can't be zero What is the denominator? 5 2/5=0

More products