Integer Division and Truncation
The central test is x % y == 0. When this comparison is true, x is divisible by y.
A Remainder With a Purpose
The modulus operator is often introduced as a way to find what remains after division. It is more useful than that description suggests. A remainder can answer yes-or-no questions about numbers, such as whether one number divides evenly into another. The same operation can also separate the rightmost digits from a larger number.
The central test is x % y == 0. When this comparison is true, x is divisible by y.
Testing Even Division
To test whether x divides evenly into y, calculate y % x and inspect the result. A result of zero means that no remainder is left, so y is divisible by x. A nonzero result means that some amount remains, so y is not divisible by x. This pattern is useful when categorizing numbers, checking whether a number is even, or validating that a quantity is a multiple of a package size.
Checking a Package Quantity
A program needs to determine whether 24 items can be grouped into packages of 6 without any items left over.
- 1Choose the modulus expression
Use 24 % 6 because 24 is the quantity being tested and 6 is the package size.
- 2Inspect the remainder
The remainder is 0, so the quantity divides evenly into groups of that size.
- 3Make the decision
The program can classify 24 as a multiple of 6.
24 is divisible by 6 because 24 % 6 == 0.
Isolating the Rightmost Digit
Modulus can also isolate digits. For a number x, x % 10 extracts the rightmost digit. For example, 4729 % 10 gives 9. The divisor 10 separates the final digit from all the digits to its left.
Taking One Digit
Find the rightmost digit of 8356.
- 1Apply modulus 10
Calculate 8356 % 10.
- 2Read the result
The result is 6, which is the rightmost digit of 8356.
8356 % 10 gives 6.
Keeping Several Digits
Changing the divisor changes how many rightmost digits are retained. Modulus 10 extracts the last digit, modulus 100 extracts the last two digits, and modulus 1000 extracts the last three digits. The divisor determines the size of the rightmost portion that remains.
| Expression | Digits retained | Result |
|---|---|---|
| 4729 % 10 | rightmost digit | 9 |
| 4729 % 100 | rightmost two digits | 29 |
| 4729 % 1000 | rightmost three digits | 729 |
The divisor controls how many rightmost digits remain.
Shrinking a Number Digit by Digit
To process every digit, combine modulus with integer division. First use modulus 10 to obtain the current rightmost digit. Then use integer division to remove that rightmost digit from the number. Repeat the process while the number shrinks. Starting with 4729, the extracted digits are 9, then 2, then 7, then 4. They appear in reverse order because the rightmost digit is always processed first.
Processing 4729
Extract every digit from 4729 using modulus 10 and integer division.
- 1First step
The current number is 4729. Modulus 10 extracts 9, and integer division removes that digit so the number becomes 472.
- 2Second step
The current number is 472. Modulus 10 extracts 2, and integer division leaves 47.
- 3Third step
The current number is 47. Modulus 10 extracts 7, and integer division leaves 4.
- 4Fourth step
The current number is 4. Modulus 10 extracts 4, and integer division leaves 0.
The digits are extracted in the order 9, 2, 7, 4. To obtain forward order, store them in a list or string and reverse it afterward.
From Pattern to Program
These two patterns support practical programming tasks. A divisibility check can categorize numbers, such as identifying numbers divisible by 4, identifying even numbers divisible by 2, or validating a quantity as a multiple of a package size. Digit extraction can support summing digits, checking whether a number is a palindrome, and validating credit card checksums. In each case, modulus supplies either a decision-making remainder or a piece of the number.
Suppose a quantity must be packed in groups of 8. The program can calculate quantity % 8. A zero result means the quantity fits the package size exactly. A nonzero result identifies a quantity that does not form complete groups. The same reasoning applies when categorizing even numbers with divisibility by 2.
Common Mistakes
Common mistake Using a nonzero remainder as evidence of divisibility.
What it looks likeTreating 13 % 3 as proof that 13 is divisible by 3.
Why it failsA nonzero remainder means that some amount is left over.
Do this insteadA divisibility test succeeds only when the modulus result equals zero.
Common mistake Using modulus 10 when several rightmost digits are needed.
What it looks likeUsing 4729 % 10 when the desired result is 29.
Why it failsModulus 10 keeps only the rightmost digit.
Do this insteadUse modulus 100 for the last two digits and modulus 1000 for the last three digits.
Common mistake Expecting digit extraction to produce digits from left to right.
What it looks likeExpecting the number 4729 to produce 4, 7, 2, 9 when repeatedly using modulus 10.
Why it failsThe rightmost digit is extracted first, so the natural order is 9, 2, 7, 4.
Do this insteadStore the extracted digits and reverse them afterward if forward order is required.
Common mistake Extracting a digit without shrinking the number.
What it looks likeRepeatedly applying modulus 10 to the same unchanged number.
Why it failsThe same rightmost digit will be selected again.
Do this insteadAfter extracting the digit, use integer division to remove it before the next iteration.
Practice the Patterns
For each prompt, write the modulus expression and state what the result tells you. First, determine whether 45 is divisible by 5. Next, extract the rightmost digit of 6381. Then determine which expression extracts the last two digits of 6381. Finally, list the digits produced when 6381 is processed repeatedly from right to left using modulus 10 and integer division.
Hints
- For divisibility, compare the modulus result with zero.
- Use modulus 10 for one rightmost digit and modulus 100 for two.
- The repeated digit process begins with the rightmost digit.
Key Takeaways
- 1A number x is divisible by y when x % y == 0.
- 2Modulus 10 extracts the rightmost digit.
- 3Modulus 100 and modulus 1000 extract the last two and last three digits.
- 4Combining modulus with integer division lets a program process digits sequentially.
- 5Repeated digit extraction starts from the right, so the digits appear in reverse order unless they are stored and reversed.
At a glance
- Use modulus to determine whether division is even: a zero remainder means divisible.
- Use modulus 10 to isolate the rightmost digit.
- Use modulus 100 or 1000 to retain multiple rightmost digits.
- Combine modulus with integer division to process every digit from right to left.
- These patterns support validation, categorization, and numeric analysis.