math in java – what does ” %” do?

% is the Modulus operator. For Java Modulus:

"%  Modulus - Divides left hand operand by right hand operand and returns remainder"

For example: 10 % 3 is equal to 1. To visually see this –

10 % 3
10 - 3 = 7    // Start by subtracting the right hand side of the % operator
7 - 3 = 4     // Continue subtraction on remainders
4 - 3 = 1
Now you can't subtract 3 from 4 without going negative so you stop.
You have 1 leftover as a remainder so that is your answer.

You can think of it as “How much would I have to subtract to the value on the left in order to make it evenly divisible by the right hand value?”


And yes, actually it’s the same symbol in C++ for modulus.


“In arithmetic, the remainder is the integer “left over” after dividing one integer by another to produce an integer quotient (integer division).”

“In computing, the modulo (sometimes called modulus) operation finds the remainder of division of one number by another.”

Leave a Comment