Loading...
Loading...
Loading...
A calculator app must never crash, even when a user divides by zero. Read two whole numbers a and b and print a / b using integer division.
If b is 0, Java throws an ArithmeticException. Catch it and print Cannot divide by zero instead of letting the program stop.
Cannot divide by zero.int and a try-catch, not an if check on bExample 1
Input
17 5
Output
3
17 / 5 is 3 in integer division.
Example 2
Input
8 0
Output
Cannot divide by zero
Dividing by 0 throws ArithmeticException.
Try the problem first. Open one hint at a time.
Put only the division and its print inside the try block.
Catch ArithmeticException, the exception Java throws for an integer divided by 0.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor