Java ProgramsControl FlowReverse a Number

Reverse a Number in Java

beginner·  Control Flow  ·  Loops

Problem

Reversing a number's digits means rebuilding it with the last digit first, by peeling digits off one end and appending them onto a new running value.

Given a number, reverse the order of its digits.

Input
1234
Output
Reversed number: 4321

Java Program

Java
public class ReverseNumber { public static void main(String[] args) { int n = 1234; int reversed = 0; while (n > 0) { reversed = reversed * 10 + n % 10; // shift left and append the last digit n /= 10; } System.out.println("Reversed number: " + reversed); } }

Output

Reversed number: 4321

Core Logic

Shifting the running result one place left and appending the next extracted digit rebuilds the number with its digits in reverse order.

How It Works
  1. 1n % 10 extracts the current last digit of n.
  2. 2reversed * 10 + digit shifts every digit already in reversed one place left, then appends the newly extracted digit.
  3. 3n /= 10 removes the digit just processed, and the loop continues until n reaches 0.
For 1234, the digits 4, 3, 2, 1 are extracted in that order and appended in turn, building reversed up to 4321.
💡

Key Point: Each digit is appended in the exact order it's extracted — since digits come off from the end first, appending them in that order naturally reverses the whole number.

Complexity
Time Complexity: O(d)Space Complexity: O(1)

Why: Each loop iteration strips one digit off n and appends it to the running result, so the number of iterations equals n's digit count d.

Key Concepts

while loopmodulo operatordigit extraction

Approach 2: Using StringBuilder.reverse()

Java
public class ReverseNumberStringBuilder { public static void main(String[] args) { int n = 1234; // Reverses the digits via a String instead of arithmetic int reversed = Integer.parseInt(new StringBuilder(String.valueOf(n)).reverse().toString()); System.out.println("Reversed number: " + reversed); } }

Output

Reversed number: 4321

Core Logic

Converting the number to a String and reversing it with the built-in StringBuilder.reverse() skips the manual digit-by-digit arithmetic entirely.

How It Works
  1. 1String.valueOf(n) converts the number into its digit string.
  2. 2new StringBuilder(...).reverse() flips the digit string's order in place.
  3. 3Integer.parseInt(...) converts the reversed string back into an int.
For 1234, new StringBuilder("1234").reverse().toString() gives "4321", parsed back into the same 4321 the manual version computes.
💡

Key Point: This is a matter of style, not efficiency — the StringBuilder version reads a bit more directly at the cost of allocating a small String and buffer, where the arithmetic version works with just primitives.

Complexity
Time Complexity: O(d)Space Complexity: O(d)

Why: Reversing through a String still visits each of the d digits once, but this version allocates a String and StringBuilder buffer proportional to the digit count, unlike the arithmetic version's constant space.

Key Concepts

StringBuilderreverse()String-based reversal

Related Programs