Start learning free

LESSON 25 OF 415 · CALCULATIONS WITH NUMBERS

The Modulo Operator in Swift: Finding Remainders with %

Lesson Read the full explanation

TL ; DR:

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

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

  • It can also be used with variables:

    var a = 2
    var b = 4
    
    print(b % a) // Output: 0
    print(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:

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

It can also be used with variables:

var a = 2
var b = 4

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

floor() function

The floor() function returns the largest integer less than or equal to a given number (it needs import Foundation and returns a Double, so we wrap it in Int(...)):

import Foundation

print(Int(floor(3.9))) // Output: 3
print(Int(floor(5.2))) // Output: 5
print(Int(floor(7.0))) // Output: 7

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

var a = 2
var b = 3

var c = b / a
print(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:
var quotient = 26 / 3
var remainder = 26 % 3

print(quotient) // Output: 8
print(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

    SWIFT · CALCULATIONS WITH NUMBERS

    Modulo Operator in Swift, explained

    What the operator does

    The % operator gives the remainder of a division. Dividing 15 by 4 fits 4 into 15 three times (12), and 3 is left over, so 15 % 4 is 3. Swift calls this the remainder operator, and in everyday programming talk it is often called modulo.

    print(10 % 2)   // 0
    print(15 % 4)   // 3
    print(20 % 3)   // 2
    

    Operands can be variables too:

    let a = 2
    let b = 4
    
    print(b % a)    // 0
    print(11 % b)   // 3
    

    A remainder of 0 means the first number divides evenly by the second. That makes % the usual way to test whether a number is even: n % 2 == 0 is true for 4 and 0, and false for 7.

    Quotient and remainder together

    Division and remainder describe the same split. For two Int values, / gives the quotient and % gives the remainder:

    let quotient = 26 / 3
    let remainder = 26 % 3
    
    print(quotient)    // 8
    print(remainder)   // 2
    

    They always fit together: 26 = 3 * 8 + 2.

    Wrapping around

    The remainder also keeps a value inside a range. On a 24-hour clock, 5 hours after 22:00 is 3:00, and the remainder computes it: (22 + 5) % 24 is 3. For non-negative values the result is always between 0 and one less than the divisor, so it never leaves the clock.

    Swift specifics

    • Integers only. % works on Int and the other integer types. 7.5 % 2 doesn't compile; the error message tells you to use truncatingRemainder. 7.5.truncatingRemainder(dividingBy: 2) is 1.5.
    • The sign follows the first operand. -7 % 3 is -1, and 7 % -3 is 1. If you need a result that is never negative, for example when wrapping around, you must adjust it yourself.
    • A zero divisor stops the program. x % 0 is a runtime error for Int values.
    • A smaller first operand. 5 % 7 is 5: 7 fits zero times and everything is left over.
    • Same precedence as * and /. They run from left to right, before + and -.

    The exercise

    Andy has amount dollars and each product costs price dollars. He buys as many whole products as he can. The task is to print the money he has left afterwards. The starter code is:

    let amount = 70
    let price = 15
    

    Andy can buy 4 products for 60 dollars, and 10 dollars remain. That leftover is the remainder of amount divided by price, so % computes it directly, without finding the number of products first.

    The solution in Swift

    let amount = 70
    let price = 15
    
    print(amount % price)
    

    The output is:

    10
    

    Common mistakes

    • Using / instead of %. amount / price is 4, the number of products, not the money left.
    • Swapping the operands. price % amount is 15, because 70 doesn't fit in 15 even once.
    • Reading % as a percentage. Swift has no percentage operator. % only produces remainders.
    • Expecting the remainder to be negative-safe. A negative first operand produces a negative remainder, which can surprise code that uses the result as an index.

    Testing

    • amount = 70, price = 15 prints 10.
    • amount = 17, price = 5 prints 2.
    • amount = 30, price = 10 prints 0.
    • amount = 5, price = 7 prints 5.

    Key takeaways

    • a % b is what is left after dividing a by b as many whole times as possible.
    • A remainder of 0 means an even split.
    • In Swift, % is for integers; the remainder takes the sign of the first operand.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free