Working with Even and Odd Numbers
The modulus operator (%) returns the remainder when one integer is divided by another.
The Leftover in a Division
When you divide objects into equal groups, the division may leave some objects ungrouped. For example, 7 apples can make 2 complete groups of 3, with 1 apple left over. The modulus operator, written as %, gives you that leftover amount. It does not give you the number of complete groups.
For 7 % 3, the dividend is 7 and the divisor is 3. The divisor tells us the group size, and the result is the amount left after making as many complete groups as possible.
Reading Modulus Expressions
In Python, the syntax is a % b. The value a is the dividend, and b is the divisor. The expression asks: after dividing a by b, what amount remains? If a divides evenly by b, the remainder is 0. For a positive divisor, the result is between 0 and b - 1.
| Expression | What it reports | Meaning for 7 divided by 3 |
|---|---|---|
| 7 // 3 | Complete groups | 2 complete groups |
| 7 % 3 | Leftover amount | 1 left over |
| 7 / 3 | Decimal division result | The decimal result of the division |
Predicting Remainders
What do you think happens?
What is the remainder in each expression: 5 % 8, 8 % 5, and 10 % 5?
Reveal answer
Answer: 5, 3, 0
In 5 % 8, the dividend is smaller than the divisor, so no complete group of 8 can be made and all 5 remain. In 8 % 5, one group of 5 leaves 3. In 10 % 5, the division is even, so the remainder is 0.
Finding the remainder
Determine the results of 7 % 3, 10 % 4, 12 % 6, and 5 % 8.
7 % 3: Two complete groups of 3 use 6, leaving 1.
10 % 4: Two complete groups of 4 use 8, leaving 2.
12 % 6: Two complete groups of 6 use all 12, leaving 0.
5 % 8: The dividend 5 is smaller than the divisor 8, so no complete group of 8 can be made. The entire amount left over is 5.
7 % 3 is 1, 10 % 4 is 2, 12 % 6 is 0, and 5 % 8 is 5.
Even and Odd Checks
Dividing by 2 creates a simple pattern. An even number divides evenly by 2, so number % 2 is 0. An odd number leaves 1 after division by 2, so number % 2 is 1. A program can use this remainder check to distinguish the two patterns.
| Number | Remainder from number % 2 | Pattern |
|---|---|---|
| 4 | 0 | Even |
| 5 | 1 | Odd |
| 8 | 0 | Even |
| 9 | 1 | Odd |
The remainder from division by 2 identifies the even-or-odd pattern.
Repeating Modulus Patterns
Using the same divisor on an increasing sequence creates a repeating cycle of remainders. With a divisor of 3, the cycle is 0, 1, 2, 0, 1, 2, and so on. The remainders never exceed 2, which is one less than the divisor.
- Detecting even and odd numbers.
- Checking whether a number is divisible by another number.
- Distributing items into repeating groups or buckets.
- Calculating positions in circular structures such as clocks or calendars.
Mistakes with Remainders
Treating % as the number of complete groups.
The value 2 describes the complete groups, not the leftover.
Fix:
Use 7 // 3 for the complete groups and 7 % 3 for the leftover, which is 1.Assuming a smaller dividend must produce a remainder of 0.
No complete group of 8 fits into 5, so all 5 remain.
Fix:
When the dividend is smaller than the divisor, the dividend is the leftover in this case: 5 % 8 is 5.Confusing a remainder of 0 with a remainder of 1 when checking parity.
A remainder of 0 means division by 2 is exact; a remainder of 1 indicates the odd pattern.
Fix:
Check for 0 to identify even numbers and 1 to identify odd numbers.
Practice with Modulus
Predict the result of each expression before calculating it: 14 % 4, 18 % 6, 3 % 7, and 11 % 2. Then identify which of the numbers 14, 18, 3, and 11 follow the even pattern when checked with % 2.
Hints
- Find the largest number of complete divisor-sized groups first.
- The remainder is what is left after those complete groups.
- For the even-or-odd check, compare number % 2 with 0 and 1.
- Identify the dividend and divisor in a % b.
- Determine how many complete groups of b fit into a.
- Find the amount left after those groups.
- Use that leftover as the modulus result.
- For an even-or-odd check, divide by 2 with % and inspect whether the result is 0 or 1.
Key Takeaways
- The modulus operator % returns the remainder left after one integer is divided by another.
- In a % b, a is the dividend and b is the divisor.
- A remainder of 0 means the division is even; with division by 2, a remainder of 1 identifies the odd pattern.
- Modulus results follow repeating cycles, which makes them useful for divisibility, grouping, circular positions, and pattern detection.
- Do not confuse modulus with floor division: // reports complete groups, while % reports the leftover.