Skip to content
ADevGuide Logo ADevGuide
Go back

Reverse a Number in Java: Loop Logic and Code Example

Updated:

By Pratik Bhuite | 4 min read

Hub: Java / Interview Fundamentals

Series: Java Interview & Problem Solving Series

Last updated: Aug 30, 2026

Part 2 of 5 in the Java Interview & Problem Solving Series

Key Takeaways

On this page
Reading Comfort:

Reverse a Number in Java: Loop Logic and Code Example

Reverse Integer in Java

Learning to reverse a number in Java is a common beginner interview problem because it tests loops, modulo arithmetic, and basic number manipulation. This example shows how to reverse a number in Java without using helper APIs, using simple while-loop logic.

Interview Problem Statement

  • Reverse an integer in Java without using any API.
  • Reverse a number in Java.

Java Regex Quiz: 20 MCQ Questions with Answers

Code Example

package com.adevguide.java.problems;

public class ReverseInteger {

public static void main(String\[\] args) {
    // Variable to hold input value
    Integer input = 123;
    // Variable to hold reversed value
    Integer reverse = 0;
    // loop till input value becomes zero
    while (input != 0) {
        // multiple previous reverse value by 10 , add the remainder to it and save it back
        // in reverse variable
        reverse = reverse \* 10 + input % 10;
        // Divide input by 10 and store quotient value in input
        input = input / 10;
    }
    // Print reverse Value
    System.out.println("Reverse Integer ::" + reverse);
}

}

Code Explanation

To reverse an integer in Java, we will follow the following steps:

  1. Find one’s place of the input and add it to 10 x previously calculated reverse value.
  2. Change input value to the quotient of division of input by 10.

Example:

Input = 123

Iteration 1:

reverse = 0 * 10 + 123 % 10

reverse = 3

Input = 12

Iteration 2:

reverse = 3 * 10 + 12 % 10

reverse = 32

Input = 1

Iteration 3:

reverse = 32 * 10 + 1 % 10

reverse = 321

Input = 0

Source Code

As always, you can find all our source code at GitHub.

GitHub Link

YouTube Videos

  1. “Reverse Number Program in Java - [updated 2025] Interview Prep | Java Programming Tutorial” https://www.youtube.com/watch?v=I-saO_f6NEU

  2. “Java Program to Reverse a Number with Explanation” https://www.youtube.com/watch?v=pZ3HeOwglwQ

  3. “LeetCode #7: Reverse Integer (Java) | Don’t Fall for the Overflow Trap!” https://www.youtube.com/watch?v=WV90AnTVSY0


Share this post on:

Next in Series

Continue through the [object Object] with the next recommended article.

Related Posts

Keep Learning with New Posts

Subscribe through RSS and follow the project to get new series updates.

Was this guide helpful?

Share detailed feedback

Previous Post
Tomcat vs JBoss WildFly: Differences and Use Cases
Next Post
How to Dockerize Java Application with Maven and Dockerfile