Skip to content
ADevGuide Logo ADevGuide
Go back

Reverse Integer in Java: Simple Explanation and Code

Updated:

By Pratik Bhuite | 3 min read

Hub: Java / Interview Fundamentals

Series: Java Interview & Problem Solving Series

Last updated: Mar 26, 2026

Part 3 of 11 in the Java Interview & Problem Solving Series

Key Takeaways

On this page
Reading Comfort:

Reverse Integer in Java: Simple Explanation and Code

Reverse Integer in Java

Reversing an integer in Java is a common beginner interview problem because it tests loops, modulo arithmetic, and basic number manipulation. This example shows the standard approach without using helper APIs.

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


Share this post on:

Next in Series

Continue through the Java Interview & Problem Solving Series 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
Java String Quiz: 16 MCQ Questions with Answers
Next Post
Kids With the Greatest Number of Candies: LeetCode Java Solution