Concepts / Working with Strings and Numbers

Working with Strings and Numbers

First, it can ask whether one number divides evenly into another.

  • 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.

Key point

The expression x % y == 0 means that x is divisible by y.

12 ÷ 3remainder 0Divisible12 % 3 == 013 ÷ 3remainder not 0Not divisible13 % 3 != 0modulus result is 0modulus result is not 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.

4729number% 10keep the final digit9rightmost digitapplyresult
Why does taking a number modulo 10 leave exactly its rightmost digit?
Worked example

Finding the Final Digit

Determine the rightmost digit of 5836 using modulus.

  1. 1
    Choose the divisor

    Use 10 because modulus 10 extracts the rightmost digit.

  2. 2
    Apply modulus

    Evaluate 5836 % 10.

  3. 3
    Read the result

    The result is 6, so 6 is the rightmost digit.

Result

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.

4729original number94729 % 10294729 % 1007294729 % 1000use 10use 100use 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.

4729extract 9472extract 247extract 74extract 40finishedinteger division removes 9integer division removes 2integer division removes 7integer division removes 4
How does the number shrink as each rightmost digit is extracted?
Worked example

Reading Every Digit

Process the digits of 4729 from right to left.

  1. 1
    First iteration

    Modulus 10 extracts 9. Integer division removes that digit, leaving 472.

  2. 2
    Second iteration

    Modulus 10 extracts 2. Integer division leaves 47.

  3. 3
    Third iteration

    Modulus 10 extracts 7. Integer division leaves 4.

  4. 4
    Fourth iteration

    Modulus 10 extracts 4. Integer division leaves 0, so processing is complete.

Result

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.

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

Frequent Mistakes

  • Common mistake

    Treating every modulus result as a divisibility confirmation.

    What it looks like

    A nonzero result from x % y does not mean that x is divisible by y.

    Why it fails

    Divisibility is confirmed specifically when the modulus result equals 0.

    Do this instead

    Use the condition x % y == 0 when checking whether x divides evenly by y.

  • Common mistake

    Using the wrong divisor for digit extraction.

    What it looks like

    Using modulus 100 when only the rightmost digit is needed.

    Why it fails

    Modulus 100 extracts the last two digits, not only the final digit.

    Do this instead

    Use modulus 10 for one digit, modulus 100 for two digits, and modulus 1000 for three digits.

  • Common mistake

    Expecting repeated rightmost-digit extraction to produce digits from left to right.

    What it looks like

    Processing 4729 produces 9, 2, 7, 4.

    Why it fails

    The method always takes the rightmost digit first.

    Do this instead

    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.
Worked example

Practice Check

Check the results for 6384.

  1. 1
    Divisibility by 2

    6384 % 2 equals 0, so 6384 is divisible by 2.

  2. 2
    Rightmost digit

    6384 % 10 equals 4, so the rightmost digit is 4.

  3. 3
    Last two digits

    6384 % 100 equals 84, so the last two digits are 84.

Result

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.

Related concepts

More concepts starting with W