Loading...
Loading...
Loading...
Friends split a restaurant bill equally in whole rupees. Read the bill and the number of friends as text, then convert them.
Each pays: X and Left over: R (the rupees that cannot be shared equally).Please enter whole numbers.Friends cannot be zero.Whatever happens, the last line must be Bill closed, printed from a finally block.
Bill closed.Example 1
Input
1000 3
Output
Each pays: 333 Left over: 1 Bill closed
1000 / 3 = 333, and 1000 % 3 = 1.
Example 2
Input
500 0
Output
Friends cannot be zero Bill closed
finally runs after the catch block too.
Try the problem first. Open one hint at a time.
Read both lines as text with nextLine() before the try block.
Use two catch blocks: NumberFormatException for bad text and ArithmeticException for dividing by 0.
Put the Bill closed print in finally, not after the try-catch, to practise how finally works.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor