Start learning free

LESSON 25 OF 414 · CALCULATIONS WITH NUMBERS

The Modulo Operator in Scala: Finding Remainders with %

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

Integer division

When both numbers are Ints, the / operator drops the fractional part and gives the integral part of the division (the quotient):

println(10 / 2) // Output: 5
println(15 / 4) // Output: 3
println(20 / 3) // Output: 6

It can also be used with variables:

val a = 2
val b = 3

val c = b / a
println(c) // Output: 1

Quotient and remainder:

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

// Let's divide 26 by 3:
val quotient = 26 / 3
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

    SCALA · CALCULATIONS WITH NUMBERS

    Modulo Operator in Scala, explained

    What modulo does

    The modulo operator % gives the remainder of a division, the part that does not fit evenly. If you divide 15 by 4, the divisor fits 3 times (that is 12), and 3 is left over. So 15 % 4 is 3.

    println(10 % 2)
    println(15 % 4)
    println(20 % 3)
    
    0
    3
    2
    

    It works with variables in the same way:

    var a = 2
    var b = 4
    println(b % a)
    println(11 % b)
    
    0
    3
    

    A remainder of 0 means the first number divides evenly by the second. That makes x % 2 == 0 the usual test for an even number.

    Quotient and remainder together

    In Scala, / on two Int values gives the quotient and % gives the remainder. They always fit together: the divisor times the quotient, plus the remainder, gives back the original number.

    val quotient = 26 / 3
    val remainder = 26 % 3
    println(quotient)
    println(remainder)
    println(quotient * 3 + remainder)
    
    8
    2
    26
    

    Dividing 26 by 3 gives 8 groups of 3, which is 24, and 2 is left over. The last line confirms the identity.

    Details specific to Scala

    • Negative numbers. The result takes the sign of the left operand: -7 % 2 is -1, and 7 % -2 is 1. If you need a non-negative remainder for a positive divisor, use Math.floorMod(-7, 2), which gives 1.
    • Doubles. % also works on decimal numbers: 5.5 % 2 is 1.5. It does the same job, the part left after taking away whole multiples of the divisor.
    • Zero divisor. 10 % 0 with Int values throws java.lang.ArithmeticException: / by zero. With a Double zero it gives NaN.
    • Precedence. % has the same rank as * and /, so it is applied before + and -, and from left to right among equals.

    You may meet examples that use Math.floor to compute a quotient. In Scala this is not needed for integers, because / on Int values already gives the whole quotient. Math.floor returns a Double, so Math.floor(26 / 3) prints 8.0 rather than 8.

    Two common uses

    Even or odd. Every even number leaves remainder 0 when divided by 2, and every odd number leaves 1, so 10 % 2 == 0 is true and 7 % 2 == 0 is false. For an odd test on possibly negative numbers, compare with 0 instead: x % 2 != 0, because -7 % 2 is -1, and -7 % 2 == 1 is false.

    Wrapping around. A value that cycles, such as the hours on a 24-hour clock, can be kept in range with %. Five hours after 22:00 is (22 + 5) % 24, which is 3, so 03:00.

    The exercise

    Andy has amount dollars and each product costs price dollars. He buys as many products as he can. The money left after that is whatever does not divide evenly by the price, which is the remainder. For 70 dollars and a price of 15, he buys 4 products, which cost 60, and 10 dollars remain:

    val amount = 70
    val price = 15
    println(amount % price)
    

    The output is:

    10
    

    Both values are inferred as Int, which is why % gives an exact whole-number remainder. The number of products itself would be amount / price, which is 4.

    Common mistakes

    • Using / instead of %. amount / price is the number of products, 4, not the money left.
    • Swapping the operands. price % amount is 15 % 70, which is 15. The left operand is the number being divided.
    • Expecting a percentage. The symbol is the same in other contexts, but in Scala % always means remainder.

    Key takeaways

    • a % b is the remainder of dividing a by b.
    • For Int values, a / b and a % b are the quotient and the remainder.
    • A remainder of 0 means the division is exact.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free