Digit Manipulation (% and /)

Digit manipulation means taking a number apart one digit at a time using two tiny operations: n % 10 gives the last digit, and n / 10 (integer division) removes it. Interviewers like it because it tests whether you can work with numbers without converting them to strings, and it hides classic traps such as negative numbers, trailing zeros, and integer overflow.

When to use it

  • The question asks you to reverse, sum, count, or rearrange the digits of an integer.
  • It says 'without converting the integer to a string'.
  • You need to check a number property like palindrome, Armstrong number, or happy number.
  • You must build a new number digit by digit, such as result = result * 10 + digit.
  • The problem mentions 32-bit integer limits or overflow when reversing or adding.

See it work

  1. Goal: reverse the number 1203. Start with n = 1203 and rev = 0. We will peel the last digit off n and push it onto rev. Digits are shown as cells, with the pointer on the digit we are about to peel. [1, 2, 0, 3]
  2. Step 1: digit = 1203 % 10 = 3. Push it: rev = 0 * 10 + 3 = 3. Then drop it from n: n = 1203 / 10 = 120. [1, 2, 0, 3]
  3. Step 2: digit = 120 % 10 = 0. Push it: rev = 3 * 10 + 0 = 30. Then n = 120 / 10 = 12. The zero was not lost, it just became a trailing zero of rev. [1, 2, 0, 3]
  4. Step 3: digit = 12 % 10 = 2. Push it: rev = 30 * 10 + 2 = 302. Then n = 12 / 10 = 1. [1, 2, 0, 3]
  5. Step 4: digit = 1 % 10 = 1. Push it: rev = 302 * 10 + 1 = 3021. Then n = 1 / 10 = 0, so the loop stops because n is 0. [1, 2, 0, 3]
  6. Result: 1203 reversed is 3021. The same loop shape works for summing digits (sum += digit), counting digits (count++), or checking palindromes (compare rev to the original). [3, 0, 2, 1]

Code template

Python

# Reverse the digits of a non-negative int
def reverse_digits(n):
    rev = 0
    while n > 0:
        digit = n % 10          # last digit
        rev = rev * 10 + digit  # push it onto the result
        n //= 10                # drop the last digit (use // in Python)
    return rev

C

// Reverse the digits of a non-negative int (add overflow check for real use)
long reverse_digits(int n) {
    long rev = 0;
    while (n > 0) {
        int digit = n % 10;      // last digit
        rev = rev * 10 + digit;  // push it onto the result
        n /= 10;                 // drop the last digit
    }
    return rev;
}

Java

// Reverse the digits of a non-negative int (long avoids overflow here)
static long reverseDigits(int n) {
    long rev = 0;
    while (n > 0) {
        int digit = n % 10;      // last digit
        rev = rev * 10 + digit;  // push it onto the result
        n /= 10;                 // drop the last digit
    }
    return rev;
}

Complexity: Time O(log10 n), which is the number of digits in n; space O(1) because you only keep a few integer variables.

Programs that use it

Interview problems that use it

  • Reverse Integer: Peel digits with % 10 and build the reversed value with rev * 10 + digit, checking against the 32-bit limits before each push.
  • Palindrome Number: Reverse only the second half of the digits and compare it to the first half, which also avoids overflow.
  • Happy Number: Repeatedly replace the number with the sum of the squares of its digits, and detect a loop with a set or fast/slow pointers.
  • Add Digits (Digital Root): Sum the digits in a loop until a single digit remains, or use the formula 1 + (n - 1) % 9.
  • Armstrong Number: Count the digits first, then peel each digit and add digit raised to that count, and compare the total to the original.
  • Count Digits That Divide the Number: Peel each digit off a copy of n and count it if it is nonzero and n % digit == 0.

Common mistakes

  • Modifying the original number and then forgetting to save a copy, so you have nothing left to compare against (for example in palindrome or Armstrong checks).
  • Ignoring overflow: rev * 10 + digit can exceed the 32-bit range, so check before multiplying or use a wider type like long.
  • Mishandling negatives and division: in C and Java, -123 % 10 is -3, and in Python // rounds toward negative infinity, so handle the sign separately or work with the absolute value.

Frequently asked questions

Why use % 10 and / 10 instead of converting the number to a string?

It uses O(1) extra space, avoids string allocation, and is often explicitly required by the problem. String conversion is fine for quick scripts, but interviewers use this pattern to check that you understand how numbers are built from digits.

How do I count the digits of a number?

Loop while n > 0, increment a counter, and do n /= 10. Handle n = 0 as a special case, since the loop never runs but 0 has one digit. You can also use floor(log10(n)) + 1 for positive n.

How do I handle overflow when reversing an integer?

Before computing rev * 10 + digit, check whether rev is already greater than MAX / 10 (or equal with a digit above 7), and do the same for the negative limit. Alternatively, accumulate in a 64-bit long and compare to the 32-bit range at the end.

← All interview patterns

Try AI Flowchart free →