Start learning free

LESSON 214 OF 671 · WHILE LOOPS - CHALLENGES

Reverse a Number in Java: Building It Digit by Digit

Practice Read the full explanation

Given a non-negative integer n, return the number you get when you write its digits in reverse order.

Zeros at the end of n move to the front of the reversed number, where they don't count: 120 reversed is 21. If n is 0, the answer is 0.

 

Example 1:

Input: n = 123
Output: 321
Explanation: The digits 1, 2, 3 read backwards are 3, 2, 1.

Example 2:

Input: n = 120
Output: 21
Explanation: Backwards the digits are 0, 2, 1, and 021 is the number 21.

Example 3:

Input: n = 7
Output: 7
Explanation: A number with one digit stays the same.

 

Constraints:

  • 0 <= n <= 109
STEP 1 OF 5
    Ctrl + Enter to check

    JAVA · WHILE LOOPS - CHALLENGES

    Reverse Number in Java, explained

    Problem overview

    The method reverseNumber receives a non-negative integer n up to one billion and returns the number written with its digits in reverse order. 123 becomes 321. A number that ends in zeros loses them: 120 reversed is 021, which is the number 21. One-digit numbers and 0 stay as they are.

    Approach: move digits from one number to another

    The last digit of n is n % 10, and n / 10 removes it. Taking digits from the right of n yields them in exactly the order the reversed number needs: the last digit of n must become the first digit of result.

    To append a digit to the end of result, shift what is already there one place left by multiplying by 10, then add the digit. For example, 32 * 10 + 1 is 321.

    Step by step

    1. Create result as an int starting at 0. For n = 0 the loop never runs, and 0 is the right answer.
    2. Write a while (n > 0) loop that takes one digit per pass.
    3. Append the last digit: result = result * 10 + n % 10.
    4. Drop that digit from n with n = n / 10.
    5. Return result after the loop.

    The solution in Java

    class Solution {
        public int reverseNumber(int n) {
            int result = 0;
            while (n > 0) {
                result = result * 10 + n % 10;
                n = n / 10;
            }
            return result;
        }
    }
    

    Tracing n = 123:

    Passn at the startresult after
    11233
    21232
    31321

    For n = 120 the first digit moved is 0, and 0 * 10 + 0 is still 0. Nothing is stored for that zero, which is why trailing zeros vanish and the method returns 21.

    Java details:

    • Operator precedence. In result * 10 + n % 10, both * and % bind tighter than +, so it reads (result * 10) + (n % 10).
    • Overflow. An int holds up to 2147483647, and exceeding it wraps silently. Every input within the limits is safe: the reversal of a number of at most 10 digits that is at most one billion stays small. The largest, 999999999, reverses to itself. An input outside the limits, 1999999999, comes out as 1410065399 instead of 9999999991, which doesn't fit in an int.

    Another way: reverse the text

    The digits can also be reversed as text: new StringBuilder("123").reverse() gives 321, and Integer.parseInt turns the text back into a number. Because Integer.parseInt("021") is 21, the trailing zeros of the original number disappear here too. The arithmetic loop avoids the conversion and builds the answer directly.

    Tracing a number that ends in zero

    For n = 120 the loop runs three times. After the passes, result and n are (0, 12), (2, 1) and (21, 0). The first pass moves the digit 0 into result, and since result was 0 it stays 0. The last pass leaves 21, and that is the answer. Each pass appends exactly one digit, so a number with three digits needs three passes, even when one of those digits is a zero that doesn't show in the final number.

    Edge cases and common mistakes

    • Trailing zeros. 120 gives 21, not 021. Integers have no leading zeros.
    • Zero. reverseNumber(0) returns 0.
    • Largest input. 1000000000 reverses to 0000000001, which is 1.
    • Multiplying after adding. (result + n % 10) * 10 adds a stray 0 at the end of the answer.
    • Forgetting to shrink n. Without n = n / 10 the loop never ends.

    Testing

    • 123 gives 321.
    • 120 gives 21.
    • 7 gives 7.
    • 0 gives 0.
    • 1000000000 gives 1.
    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free