Loading...
Loading...
Build the brain of a small calculator. It reads a number, an operator and another number, like 12 + 5, and prints the result. The operators are +, -, *, / (whole-number division) and % (remainder).
Dividing or taking a remainder by zero must print Cannot divide by zero. Any other operator must print Unknown operator.
Cannot divide by zero or Unknown operator.Example 1
Input
12 + 5
Output
17
12 + 5 = 17.
Example 2
Input
7 / 2
Output
3
Whole-number division drops the .5.
Example 3
Input
9 % 0
Output
Cannot divide by zero
A remainder by zero is not allowed.
Try the problem first. Open one hint at a time.
Read a and b as long (the product can reach 10^18) and the operator with sc.next().charAt(0).
A switch on the operator gives one case per symbol and a default for unknown ones.
In the / and % cases, check b == 0 before dividing.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor