20 divided by 6 goes 3 times with 2 remainder. So the RAPTOR expression 20 mod 6 would compute to 2. For another example,
26 divided by 7 goes 3 times with 5 remainder. So 26 mod 7 = 5.
More Examples:
- 17 divides by 5 to yield 3 with remainder 2. So, 17 mod 5 (or 17 rem 5) = 2.
- 27 mod 10 = 7, because 10 divides into 27 twice with 7 as remainder.
- Dividing 21 by 4 gives the result 5 with remainder 1. So, 21 mod 4 = 1.
- 28 mod 5 = 3 (28 divided by 5 = 5 with 3 remainder).
- 27 mod 9 = 0 (27 divides evenly by 9)
Another use is determining if a number is even or odd. If any number mod 2 = 0, then the number is even. If number mod 2 = 1, the number is an odd number.
mod operator used to detect odd/even in selection |
That's because the mod operator is usually used in a selection in a computer program.
A very practical application of the modulus operator is making change. See Modulus Operator - Part 2 for more about this. Vending machines are coded to use the modulus operator to return your correct change.