0
0
JavaProgramBeginner · 2 min read

Java Program to Reverse Number Using While Loop

To reverse a number in Java using a while loop, repeatedly extract the last digit with digit = number % 10, build the reversed number with reversed = reversed * 10 + digit, and reduce the original number by number = number / 10 until it becomes zero.
📋

Examples

Input1234
Output4321
Input1000
Output1
Input0
Output0
🧠

How to Think About It

To reverse a number, think of peeling off its last digit one by one using the remainder operator %. Then, add each digit to a new number by shifting the existing digits left (multiply by 10) and adding the new digit. Keep doing this until the original number has no digits left.
📐

Algorithm

1
Get the input number.
2
Initialize reversed number as 0.
3
While the input number is greater than 0:
4
Extract the last digit using remainder operator.
5
Add this digit to reversed number after shifting existing digits.
6
Remove the last digit from the input number by dividing by 10.
7
Return the reversed number.
💻

Code

java
public class ReverseNumber {
    public static void main(String[] args) {
        int number = 1234;
        int reversed = 0;
        while (number > 0) {
            int digit = number % 10;
            reversed = reversed * 10 + digit;
            number = number / 10;
        }
        System.out.println(reversed);
    }
}
Output
4321
🔍

Dry Run

Let's trace the number 1234 through the code to see how it reverses.

1

Initial values

number = 1234, reversed = 0

2

First loop iteration

digit = 1234 % 10 = 4; reversed = 0 * 10 + 4 = 4; number = 1234 / 10 = 123

3

Second loop iteration

digit = 123 % 10 = 3; reversed = 4 * 10 + 3 = 43; number = 123 / 10 = 12

4

Third loop iteration

digit = 12 % 10 = 2; reversed = 43 * 10 + 2 = 432; number = 12 / 10 = 1

5

Fourth loop iteration

digit = 1 % 10 = 1; reversed = 432 * 10 + 1 = 4321; number = 1 / 10 = 0

6

Loop ends

number is now 0, loop stops, reversed number is 4321

numberdigitreversed
123444
123343
122432
114321
💡

Why This Works

Step 1: Extract last digit

Using number % 10 gets the last digit of the number, like peeling one digit off.

Step 2: Build reversed number

Multiply the current reversed number by 10 to shift digits left, then add the new digit to append it.

Step 3: Remove last digit

Divide the original number by 10 to remove the last digit and prepare for the next iteration.

🔄

Alternative Approaches

Using String Conversion
java
public class ReverseNumber {
    public static void main(String[] args) {
        int number = 1234;
        String str = Integer.toString(number);
        String reversedStr = new StringBuilder(str).reverse().toString();
        int reversed = Integer.parseInt(reversedStr);
        System.out.println(reversed);
    }
}
This method is simpler but uses extra memory for strings and is less efficient than the while loop.
Using Recursion
java
public class ReverseNumber {
    public static int reverse(int number, int reversed) {
        if (number == 0) return reversed;
        return reverse(number / 10, reversed * 10 + number % 10);
    }
    public static void main(String[] args) {
        int number = 1234;
        System.out.println(reverse(number, 0));
    }
}
Recursion is elegant but can cause stack overflow for very large numbers.

Complexity: O(d) time, O(1) space

Time Complexity

The loop runs once for each digit in the number, so time grows linearly with the number of digits.

Space Complexity

Only a few integer variables are used, so space is constant regardless of input size.

Which Approach is Fastest?

The while loop method is fastest and most memory efficient compared to string conversion or recursion.

ApproachTimeSpaceBest For
While LoopO(d)O(1)Efficient numeric reversal
String ConversionO(d)O(d)Simplicity, small inputs
RecursionO(d)O(d)Elegant code, small inputs
💡
Always initialize the reversed number to zero before starting the loop.
⚠️
Forgetting to update the original number inside the while loop causes an infinite loop.