Concepts / Working with Strings and Numbers

Working with Strings and Numbers

The modulus operator checks divisibility: x % y == 0 means x is divisible by y.

  • Programming

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.

modulus result is 0modulus result is not 012 ÷ 3remainder 0Divisible12 % 3 == 013 ÷ 3remainder not 0Not divisible13 % 3 != 0
What changes when the modulus result is zero compared with when it is not zero?

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.

applyresult4729number% 10keep the final digit9rightmost digit
Why does taking a number modulo 10 leave exactly its rightmost 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.

use 10use 100use 10004729original number94729 % 10294729 % 1007294729 % 1000
How does changing the divisor from 10 to 100 or 1000 change which rightmost digits remain?
ExpressionDigits extracted
4729 % 109
4729 % 10029
4729 % 1000729

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.

integer division removes 9integer division removes 2integer division removes 7integer division removes 44729extract 9472extract 247extract 74extract 40finished
How does the number shrink as each rightmost digit is extracted?

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.

inspecttest resultyesuse divisor 10Numbernumeric inputModuluscalculate a remainderRemainder 0divides evenlyCategoryeven, multiple, ordivisibleRightmost digituse modulus 10
How can a program use a modulus result to choose an action?

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

EASY

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.