Skip to main content
Logo image

Subsection B.1 The mod function

The mod function computes the remainder after division. Its syntax is:

mod Syntax.

r = mod(a, b)
Returns the remainder r when a is divided by b. The result satisfies 0 <= r < b for positive b.
In other words, mod(a, b) answers the question: β€œafter dividing a by b, what is left over?” For example, if you divide 10 by 3 you get 3 with a remainder of 1, so mod(10, 3) returns 1.
Two of the most common uses for mod are:
Even/odd test
mod(n, 2) returns 0 when n is even and 1 when n is odd.
Divisibility test
mod(n, k) == 0 is true exactly when n is divisible by k.

🌌 Example 5. Basic mod Values.

The table below shows several calls to mod and their results.
Table 6. Sample mod Results
Expression Result Reason
mod(10, 3) 1 \(10 = 3 \times 3 + 1\)
mod(12, 4) 0 \(12 = 4 \times 3 + 0\)
mod(7, 7) 0 any number mod itself is 0
mod(5, 9) 5 \(5 = 9 \times 0 + 5\)
mod(0, 6) 0 0 divided by anything leaves remainder 0

🌌 Example 7. Checking Even and Odd Numbers.

Because mod(n, 2) is 0 for even numbers and 1 for odd numbers, we can use it inside an if-statement to classify a number:
n = 17;
if mod(n, 2) == 0
    fprintf('%d is even\n', n)
else
    fprintf('%d is odd\n', n)
end
Running this script prints 17 is odd because mod(17, 2) equals 1, not 0.

🌌 Example 8. Printing Multiples with a Loop.

A for-loop combined with mod is a natural way to select every \(k\)-th value from a range. Here we print all multiples of 3 from 1 to 20:
for n = 1:20
    if mod(n, 3) == 0
        fprintf('%d\n', n)
    end
end
The output is 3, 6, 9, 12, 15, 18. The condition mod(n, 3) == 0 is true exactly when n is divisible by 3.

Reading Questions Check Your Understanding

1.

(a) mod(9, 3) equals 0.
    mod(9, 3) returns 0.
  • True.

  • Because \(9 = 3 \times 3 + 0\text{,}\) the remainder is 0. Any number that divides evenly leaves a remainder of 0.
  • False.

  • Because \(9 = 3 \times 3 + 0\text{,}\) the remainder is 0. Any number that divides evenly leaves a remainder of 0.
(b) What does mod(7, 4) return?
What does mod(7, 4) return?
  • 3
  • \(7 = 4 \times 1 + 3\text{,}\) so the remainder is 3.
  • 1
  • 1 is the quotient (how many times 4 fits into 7), not the remainder.
  • 4
  • The remainder is always less than the divisor, so it cannot equal 4 here.
  • 0
  • 0 would mean 7 is divisible by 4, but \(7 / 4\) is not a whole number.
(c) Which expression tests divisibility by 5?
Which expression is true exactly when n is divisible by 5?
  • mod(n, 5) == 0
  • A remainder of 0 means n divides evenly by 5, which is exactly the definition of divisibility.
  • mod(5, n) == 0
  • This tests whether 5 is divisible by n, not whether n is divisible by 5.
  • mod(n, 5) == 5
  • The remainder from mod(n, 5) is always between 0 and 4, so it can never equal 5.
  • n / 5 == 0
  • This is only true when n is 0, not for all multiples of 5 like 5, 10, or 15.
(d) The result of mod is always less than the divisor.
    For a positive divisor b, mod(a, b) always returns a value that is strictly less than b.
  • True.

  • By definition, the remainder when dividing by b satisfies \(0 \leq r < b\text{.}\) Once the remainder would reach b, another full group of b can be subtracted, so the remainder resets to 0.
  • False.

  • By definition, the remainder when dividing by b satisfies \(0 \leq r < b\text{.}\) Once the remainder would reach b, another full group of b can be subtracted, so the remainder resets to 0.
(e) What does mod(0, 6) return?
What does mod(0, 6) return?
  • 0
  • Zero divided by any nonzero number gives a quotient of 0 with remainder 0.
  • 6
  • The remainder is always less than the divisor, so it cannot be 6.
  • 1
  • Zero is evenly divisible by 6 (zero times), leaving a remainder of 0, not 1.
  • undefined
  • mod(0, b) is well defined and equals 0 for any positive b.
(f) Counting multiples of 4 from 1 to 20.
How many integers from 1 to 20 satisfy mod(n, 4) == 0?
  • 5
  • The multiples of 4 in that range are 4, 8, 12, 16, and 20 β€” five values in all.
  • 4
  • Don’t forget that 20 is also divisible by 4. The multiples are 4, 8, 12, 16, 20.
  • 6
  • The next multiple after 20 would be 24, which is outside the range. Count carefully: 4, 8, 12, 16, 20.
  • 3
  • Three is too few. List them out: \(4, 8, 12, 16, 20\) β€” that is five values.
(g)
Which expression checks whether n is even?
  • mod(n,2) == 0
  • Even numbers leave remainder 0 when divided by 2.
  • mod(n,2) == 1
  • Remainder 1 identifies odd numbers, not even numbers.
  • n/2 == 0
  • This is only true when n is zero.
  • n - 2 == 0
  • This checks only whether n equals 2.
You have attempted of activities on this page.