Start learning free

LESSON 25 OF 348 · CALCULATIONS WITH NUMBERS

Modulo Operator in Kotlin

Lesson Read the full explanation

TL ; DR:

  • The modulo operator (%) calculates the remainder of dividing two values:

    println(10 % 2) // Output: 0
    println(15 % 4) // Output: 3
    println(20 % 3) // Output: 2
    

  • It can also be used with variables:

    val a = 2
    val b = 4
    
    println(b % a) // Output: 0
    println(11 % b) // Output: 3
    


Full lesson:

Remember how we first learn about the division of two integer numbers in primary school?

The quotient is the number of times a division is completed fully, while the remainder is the amount left that doesn't entirely go into the divisor.

Here are some examples:

10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2

The modulo operator:

The modulo operator (%) calculates the remainder of dividing two values:

println(10 % 2) // Output: 0
println(15 % 4) // Output: 3
println(20 % 3) // Output: 2

It can also be used with variables:

val a = 2
val b = 4

println(b % a) // Output: 0
println(11 % b) // Output: 3

Math.floor() function

The Math.floor() function returns the largest integer less than or equal to a given number:

println(Math.floor(3.9)) // Output: 3
println(Math.floor(5.2)) // Output: 5
println(Math.floor(7.0)) // Output: 7

We can use this function if we want to compute the integral part of some division (quotient):

val a = 2
val b = 3

val c = Math.floor((b / a).toDouble())
println(c) // Output: 1

Quotient and remainder:

In programming, we combine both these concepts to get the quotient and remainder of some divison:

// Let's divide 26 by 3:
val quotient = Math.floor((26 / 3).toDouble())
val remainder = 26 % 3

println(quotient) // Output: 8
println(remainder) // Output: 2

Assignment
Follow the Coding Tutorial and let's practice with quotient and remainder!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · CALCULATIONS WITH NUMBERS

    Modulo Operator in Kotlin, explained

    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 % 5 is -2 and 17 % -5 is 2. If you need a result that is never negative, use Math.floorMod(-17, 5), which gives 3.
    • It works on Double values too. 10 % 3.0 is 1.0 and 5.5 % 2 is 1.5.
    • A smaller left operand returns itself. 7 % 10 is 7, because 10 doesn't fit into 7 at all.
    • A zero divisor is an error for integers. 5 % 0 with Int values throws an ArithmeticException with the message / by zero. With Double values, 5.0 % 0.0 gives NaN.
    • Precedence. % has the same level as * and /, above + and -, so 2 + 10 % 4 is 2 + 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.

    1. Take the remainder. amount % price is the part of the money that doesn't make another whole product.
    2. 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 / price prints 4, the number of products, not the money left.
    • Swapping the operands. price % amount is 15, because 70 doesn't fit into 15.
    • Using a Double price. The result then has a decimal part: 70 % 15.0 is 10.0.
    • Mistaking % for a percentage. The symbol only means remainder in Kotlin; percentages are calculated with multiplication and division.

    Key takeaways

    • a % b is the remainder after dividing a by b.
    • 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 = 15 prints 10.
    • amount = 60, price = 15 prints 0.
    • amount = 14, price = 15 prints 14, since not even one product can be bought.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free