Working with Strings and Numbers
The modulus operator checks divisibility: x % y == 0 means x is divisible by y.
Two Questions Modulus Answers
The modulus operator helps a program inspect numbers in two useful ways. First, it can ask whether one number divides evenly into another. Second, it can break a larger number apart by extracting digits from the right. These patterns are useful in validation, categorization, and numeric analysis.
The expression x % y == 0 means that x is divisible by y.
Reading the Rightmost Digit
A number modulo 10 gives its rightmost digit. For example, applying modulus 10 to 4729 extracts 9. The divisor 10 separates the final digit from the digits to its left, so the result is the number's ones-place digit.
Finding the Final Digit
Determine the rightmost digit of 5836 using modulus.
Choose the divisor: Use 10 because modulus 10 extracts the rightmost digit.
Apply modulus: Evaluate 5836 % 10.
Read the result: The result is 6, so 6 is the rightmost digit.
The rightmost digit of 5836 is 6.
Changing the Divisor
Changing the divisor changes how many rightmost digits remain. Modulus 10 extracts the last digit, while modulus 100 extracts the last two digits. The same pattern continues with larger powers of ten: using 1000 extracts the last three digits.
| Expression | Digits extracted |
|---|---|
| 4729 % 10 | 9 |
| 4729 % 100 | 29 |
| 4729 % 1000 | 729 |
A larger divisor based on a power of ten preserves more digits on the right.
Processing Digits Sequentially
To process every digit of a number, combine modulus with integer division. Modulus 10 extracts the current rightmost digit. Integer division then removes that rightmost digit, leaving a shorter number for the next iteration.
Reading Every Digit
Process the digits of 4729 from right to left.
First iteration: Modulus 10 extracts 9. Integer division removes that digit, leaving 472.
Second iteration: Modulus 10 extracts 2. Integer division leaves 47.
Third iteration: Modulus 10 extracts 7. Integer division leaves 4.
Fourth iteration: Modulus 10 extracts 4. Integer division leaves 0, so processing is complete.
The digits are extracted in the order 9, 2, 7, 4. They appear in reverse order because the rightmost digit is taken first.
Practical Uses
Divisibility checking is useful when a program categorizes numbers. Examples include checking whether a year is divisible by 4, determining whether a number is even by checking divisibility by 2, and validating whether a quantity is a multiple of a package size.
Digit extraction supports tasks such as summing the digits of a number, checking whether a number is a palindrome, and validating credit card checksums. In each case, modulus supplies a current digit or a group of rightmost digits, while repeated processing can examine the whole number.
Frequent Mistakes
Treating every modulus result as a divisibility confirmation.
Divisibility is confirmed specifically when the modulus result equals 0.
Fix:
Use the condition x % y == 0 when checking whether x divides evenly by y.Using the wrong divisor for digit extraction.
Modulus 100 extracts the last two digits, not only the final digit.
Fix:
Use modulus 10 for one digit, modulus 100 for two digits, and modulus 1000 for three digits.Expecting repeated rightmost-digit extraction to produce digits from left to right.
The method always takes the rightmost digit first.
Fix:
Store the extracted digits and reverse them afterward when forward order is required.
Try the Pattern
For the number 6384, determine whether it is divisible by 2, find its rightmost digit, and find its last two digits using modulus.
Hints
- For divisibility by 2, check whether the modulus result is 0.
- Use modulus 10 for the rightmost digit.
- Use modulus 100 for the last two digits.
Practice Check
Check the results for 6384.
Divisibility by 2: 6384 % 2 equals 0, so 6384 is divisible by 2.
Rightmost digit: 6384 % 10 equals 4, so the rightmost digit is 4.
Last two digits: 6384 % 100 equals 84, so the last two digits are 84.
The number is divisible by 2, its rightmost digit is 4, and its last two digits are 84.
Key Takeaways
- A modulus result of 0 shows that one number divides evenly into another.
- Modulus 10 extracts the rightmost digit of a number.
- Modulus 100 and modulus 1000 extract the last two and last three digits.
- Combining modulus with integer division lets a program process all digits from right to left.
- These patterns support validation, categorization, digit sums, palindrome checks, and other numeric analysis tasks.