Concepts / Integer Division and Truncation

Integer Division and Truncation

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

  • Programming

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.

remainder is zeroremainder is nonzero12 % 3Divisible13 % 3Not divisible
How does a zero remainder differ from a nonzero remainder when deciding whether division is even?

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.

Choose the modulus expression: Use 24 % 6 because 24 is the quantity being tested and 6 is the package size.

Inspect the remainder: The remainder is 0, so the quantity divides evenly into groups of that size.

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

containscontainscontainscontainsisolated by modulus 10472949729
How does taking a number modulo 10 isolate its rightmost digit?

Taking One Digit

Find the rightmost digit of 8356.

Apply modulus 10: Calculate 8356 % 10.

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

retain 1 digitretain 2 digitsretain 3 digits47294729 % 104729 % 1004729 % 1000
How does changing the divisor from 10 to 100 or 1000 change which rightmost digits remain?
ExpressionDigits retainedResult
4729 % 10rightmost digit9
4729 % 100rightmost two digits29
4729 % 1000rightmost three digits729

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.

integer divisionmodulus 1047294729
What happens to the current number and extracted digit as the digit-processing step repeats?

Processing 4729

Extract every digit from 4729 using modulus 10 and integer division.

First step: The current number is 4729. Modulus 10 extracts 9, and integer division removes that digit so the number becomes 472.

Second step: The current number is 472. Modulus 10 extracts 2, and integer division leaves 47.

Third step: The current number is 47. Modulus 10 extracts 7, and integer division leaves 4.

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

begintest divisibilityinspect digitsremainder resultdigit resultInput numberChoose modulus useCheck remainderClassify numberExtract digitProcess digits
How do divisibility checks and extracted digits move through the steps of a practical programming problem?

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

  • Using a nonzero remainder as evidence of divisibility.

    A nonzero remainder means that some amount is left over.

    Fix: A divisibility test succeeds only when the modulus result equals zero.

  • Using modulus 10 when several rightmost digits are needed.

    Modulus 10 keeps only the rightmost digit.

    Fix: Use modulus 100 for the last two digits and modulus 1000 for the last three digits.

  • Expecting digit extraction to produce digits from left to right.

    The rightmost digit is extracted first, so the natural order is 9, 2, 7, 4.

    Fix: Store the extracted digits and reverse them afterward if forward order is required.

  • Extracting a digit without shrinking the number.

    The same rightmost digit will be selected again.

    Fix: After extracting the digit, use integer division to remove it before the next iteration.

Practice the Patterns

MEDIUM

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

  1. A number x is divisible by y when x % y == 0.
  2. Modulus 10 extracts the rightmost digit.
  3. Modulus 100 and modulus 1000 extract the last two and last three digits.
  4. Combining modulus with integer division lets a program process digits sequentially.
  5. Repeated digit extraction starts from the right, so the digits appear in reverse order unless they are stored and reversed.

Key Takeaways

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