What the modulo operator does
The modulo operator % gives the remainder of a division. When you divide one number by another, the quotient is how many times the divisor fits completely, and the remainder is what is left over:
10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2
In Kotlin, % returns that second number:
println(10 % 2) // 0
println(15 % 4) // 3
println(20 % 3) // 2
val a = 2
val b = 4
println(b % a) // 0
println(11 % b) // 3
A remainder of 0 means the first number divides evenly by the second. That is why n % 2 == 0 is the usual test for an even number.
Quotient and remainder together
Integer division and % are two halves of the same operation. The first gives the quotient, the second the remainder:
val quotient = 26 / 3
val remainder = 26 % 3
println(quotient) // 8
println(remainder) // 2
They are linked by the identity quotient * divisor + remainder == dividend: 8 times 3 plus 2 is 26.
Details to know
- The sign follows the left operand.
-17 % 5is-2and17 % -5is2. If you need a result that is never negative, useMath.floorMod(-17, 5), which gives 3. - It works on
Doublevalues too.10 % 3.0is1.0and5.5 % 2is1.5. - A smaller left operand returns itself.
7 % 10is 7, because 10 doesn't fit into 7 at all. - A zero divisor is an error for integers.
5 % 0withIntvalues throws anArithmeticExceptionwith the message/ by zero. WithDoublevalues,5.0 % 0.0givesNaN. - Precedence.
%has the same level as*and/, above+and-, so2 + 10 % 4is2 + 2, which is 4.
Where remainders are useful
Remainders appear whenever something repeats in a cycle. n % 2 tells even from odd. minutes % 60 gives the minutes part of a duration, and day % 7 wraps around a week. A counter that must stay between 0 and 9 can use % 10. Because a remainder by b is always smaller than b for non-negative numbers, it is a compact way to keep a value within a range.
The exercise
Andy has amount dollars (70), and each product costs price dollars (15). He buys as many whole products as he can. The task is to print how much money is left.
That leftover is exactly the remainder of dividing amount by price. Seventy dollars buys 4 products, which cost 60, and 10 remain.
- Take the remainder.
amount % priceis the part of the money that doesn't make another whole product. - Print it with
println.
val amount = 70
val price = 15
println(amount % price)
The program prints 10. Integer values are inferred as Int from the literals, so no type annotation is needed.
Two worked uses
Splitting 200 minutes into hours and minutes uses both halves of a division: 200 / 60 is 3 whole hours, and 200 % 60 is the 20 minutes left. Wrapping around a cycle works the same way. On a seven-day week numbered 0 to 6, adding 3 days to day 5 lands on (5 + 3) % 7, which is day 1.
A parity check is the shortest remainder test: 7 % 2 == 0 is false, and 10 % 2 == 0 is true.
In each case, the remainder is the part of the number that the division does not use up.
Whenever a problem says "what is left", "every n-th" or "wrap around", that is a hint to reach for %.
Common mistakes
- Using
/instead of%.amount / priceprints 4, the number of products, not the money left. - Swapping the operands.
price % amountis 15, because 70 doesn't fit into 15. - Using a
Doubleprice. The result then has a decimal part:70 % 15.0is10.0. - Mistaking
%for a percentage. The symbol only means remainder in Kotlin; percentages are calculated with multiplication and division.
Key takeaways
a % bis the remainder after dividingabyb.- A remainder of 0 means the division is exact.
- The result takes the sign of the left operand.
- Integer
/gives the quotient and%the remainder, and together they rebuild the original number.
Testing
amount = 70,price = 15prints10.amount = 60,price = 15prints0.amount = 14,price = 15prints14, since not even one product can be bought.