Lesson 4 of 34

Operators: Doing Maths

8 min 20 XP

Java has the + - * / % operators. Careful: dividing two ints with / gives a whole number and drops the decimal part. % gives the remainder.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Cast with (double) when you need a decimal answer. x += 5 is short for x = x + 5. x++ adds one and x-- subtracts one.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

printf("%.2f%n", value) prints with two decimal places. %n means a new line.

Challenge

+20 XP

Read a number of minutes and print it as hours and minutes. Input: 135 → Output: 2 hours 15 minutes

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.