beginnerNumber ProblemsJava

Reverse a Number Without Using Strings

Reverse the digits of an integer using arithmetic only — no String conversion.

Quick Answer

Peel off the last digit with number % 10, append it to a running result with result = result * 10 + digit, then drop that digit using number = number / 10. Repeat until the number becomes 0. This reverses the digits using only arithmetic, no String conversion.

Problem Statement

Given an integer, return the number formed by reversing its digits. You must not convert the number to a String (or use StringBuilder.reverse()), and you should not use any library shortcut — only arithmetic operations.

For example, 1234 becomes 4321. Handle negative numbers by keeping the sign and reversing the digits of the absolute value, so -512 becomes -215.

Input: A single integer n.

Output: The integer formed by reversing the digits of n.

Examples

Example 1
Input:  1234
Output: 4321

Digits 4, 3, 2, 1 are peeled off and re-assembled.

Example 2
Input:  -512
Output: -215

The sign is preserved; the digits 5, 1, 2 reverse to 2, 1, 5.

Constraints

  • -2,147,483,648 <= n <= 2,147,483,647 (fits in a 32-bit int)
  • No String / StringBuilder conversion

Think Before You Code

Reveal the questions to ask yourself first
  • How do you read the last digit of a number without a String? (% 10)
  • How do you remove that last digit? (integer division / 10)
  • How do you grow the reversed number one digit at a time?
  • What should happen to the sign of a negative number?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
The last digit of any integer is `n % 10`. Getting it is your first step each iteration.
Hint 2
Build the answer with `result = result * 10 + lastDigit`. Multiplying by 10 shifts existing digits left and makes room for the new one.
Hint 3
After using the last digit, shrink the number with `n = n / 10`, and loop while `n != 0`. Handle the sign separately by working with the absolute value.

Approach

Reveal the step-by-step approach

Work through the number one digit at a time, from the least significant end.

  1. Remember the sign, then work with the absolute value of n.
  2. Start result at 0.
  3. While n is not 0:
    • digit = n % 10 — the current last digit.
    • result = result * 10 + digit — push it onto the reversed number.
    • n = n / 10 — drop the digit you just used.
  4. Re-apply the original sign to result.

Each * 10 shifts the digits already in result one place to the left, so the first digit you extract ends up in the highest place — exactly the reversal.

Dry Run

Walk through the example step by step

Reversing n = 1234:

step | n     | digit = n%10 | result = result*10 + digit
-----+-------+--------------+---------------------------
1    | 1234  | 4            | 0*10 + 4    = 4
2    | 123   | 3            | 4*10 + 3    = 43
3    | 12    | 2            | 43*10 + 2   = 432
4    | 1     | 1            | 432*10 + 1  = 4321
end  | 0     | —            | 4321

Solution

Reveal the full Java solution
public class ReverseNumber {
    public static int reverse(int n) {
        int sign = n < 0 ? -1 : 1;
        long value = Math.abs((long) n); // long guards against overflow
        long result = 0;
        while (value != 0) {
            int digit = (int) (value % 10);
            result = result * 10 + digit;
            value /= 10;
        }
        return (int) (sign * result);
    }

    public static void main(String[] args) {
        System.out.println(reverse(1234)); // 4321
        System.out.println(reverse(-512)); // -215
    }
}

Using a long for result and value means the multiply-by-ten step cannot silently overflow a 32-bit int while we are still building the answer. If your interviewer requires strict 32-bit handling, you would check the bounds before each result * 10 + digit and return 0 on overflow.

Time: O(d) where d is the number of digits (log10 n)Space: O(1)

Common Mistakes

  • Forgetting the sign, so -512 comes back as 215 instead of -215.
  • Using `int` for result and overflowing on inputs like 2,000,000,003.
  • Looping `while (n > 0)`, which skips negative numbers entirely.

Edge Cases to Test

  • n = 0 should return 0 (the loop body never runs).
  • Numbers ending in 0, like 1200, reverse to 21 (leading zeros disappear).
  • Integer.MIN_VALUE, whose absolute value does not fit in a positive int.

Interview Follow-Ups

  • How would you detect and handle 32-bit overflow strictly, returning 0?
  • Can you reverse the number recursively instead of with a loop?
  • How would you reverse only the digits at even positions?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us