Java reverse an int value - Principles

  1. Modding (%) the input int by 10 will extract off the rightmost digit. example: (1234 % 10) = 4

  2. Multiplying an integer by 10 will "push it left" exposing a zero to the right of that number, example: (5 * 10) = 50

  3. Dividing an integer by 10 will remove the rightmost digit. (75 / 10) = 7

Java reverse an int value - Pseudocode:

a. Extract off the rightmost digit of your input number. (1234 % 10) = 4

b. Take that digit (4) and add it into a new reversedNum.

c. Multiply reversedNum by 10 (4 * 10) = 40, this exposes a zero to the right of your (4).

d. Divide the input by 10, (removing the rightmost digit). (1234 / 10) = 123

e. Repeat at step a with 123

Java reverse an int value - Working code

public int reverseInt(int input) {
    long reversedNum = 0;
    long input_long = input;

    while (input_long != 0) {
        reversedNum = reversedNum * 10 + input_long % 10;
        input_long = input_long / 10;
    }

    if (reversedNum > Integer.MAX_VALUE || reversedNum < Integer.MIN_VALUE) {
        throw new IllegalArgumentException();
    }
    return (int) reversedNum;
}

You will never do anything like this in the real work-world. However, the process by which you use to solve it without help is what separates people who can solve problems from the ones who want to, but can't unless they are spoon fed by nice people on the blogoblags.

Answer from Eric Leschinski on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-reverse-number-program
Reverse Number Program in Java - GeeksforGeeks
... // Java program to reverse ... Function to reverse the number static void reverse(int n) { if (n <= 0) return; // remainder int rem = n % 10; rev = (rev * 10) + rem; reverse(n / 10); } // Driver Function public static void ...
Published   July 23, 2025
🌐
Programiz
programiz.com › java-programming › examples › reverse-number
Java Program to Reverse a Number
Now num = 0, so the test expression num != 0 fails and while loop exits. reversed already contains the reversed number 4321. class Main { public static void main(String[] args) { int num = 1234567, reversed = 0; for(;num != 0; num /= 10) { int digit = num % 10; reversed = reversed * 10 + digit; } System.out.println("Reversed Number: " + reversed); } }
🌐
W3Schools
w3schools.com › java › java_howto_reverse_number.asp
Java How To Reverse a Number
- num /= 10 removes the last digit from the original number. When the loop finishes, reversed contains 4321. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Baeldung
baeldung.com › home › java › java numbers › reverse a number in java
Reverse a Number in Java | Baeldung
January 4, 2026 - This is possible in three different ways: using a while loop, a for loop, or recursion. The approaches below also cater to negative values by using the absolute value of the number to be reversed and multiplying the reversed number by -1 if ...
🌐
Vultr Docs
docs.vultr.com › java › examples › reverse-a-number
Java Program to Reverse a Number | Vultr Docs
April 10, 2025 - In this code, as long as the number is not zero, the last digit is peeled off using the modulus operation, added to the reverse, and the original number is reduced by dividing by 10. This loop effectively places the last digit of the number at the front, achieving the reverse of a number in Java.
🌐
Javatpoint
javatpoint.com › how-to-reverse-a-number-in-java
How to Reverse a Number in Java - javatpoint
How to Reverse a Number in Java with java tutorial, features, history, variables, object, class, programs, operators, for-loop, oops concept, array, string, map, math, methods, examples etc.
Top answer
1 of 16
125

Java reverse an int value - Principles

  1. Modding (%) the input int by 10 will extract off the rightmost digit. example: (1234 % 10) = 4

  2. Multiplying an integer by 10 will "push it left" exposing a zero to the right of that number, example: (5 * 10) = 50

  3. Dividing an integer by 10 will remove the rightmost digit. (75 / 10) = 7

Java reverse an int value - Pseudocode:

a. Extract off the rightmost digit of your input number. (1234 % 10) = 4

b. Take that digit (4) and add it into a new reversedNum.

c. Multiply reversedNum by 10 (4 * 10) = 40, this exposes a zero to the right of your (4).

d. Divide the input by 10, (removing the rightmost digit). (1234 / 10) = 123

e. Repeat at step a with 123

Java reverse an int value - Working code

public int reverseInt(int input) {
    long reversedNum = 0;
    long input_long = input;

    while (input_long != 0) {
        reversedNum = reversedNum * 10 + input_long % 10;
        input_long = input_long / 10;
    }

    if (reversedNum > Integer.MAX_VALUE || reversedNum < Integer.MIN_VALUE) {
        throw new IllegalArgumentException();
    }
    return (int) reversedNum;
}

You will never do anything like this in the real work-world. However, the process by which you use to solve it without help is what separates people who can solve problems from the ones who want to, but can't unless they are spoon fed by nice people on the blogoblags.

2 of 16
35

I am not clear about your Odd number. The way this code works is (it is not a Java specific algorithm) Eg. input =2345 first time in the while loop rev=5 input=234 second time rev=5*10+4=54 input=23 third time rev=54*10+3 input=2 fourth time rev=543*10+2 input=0

So the reversed number is 5432. If you just want only the odd numbers in the reversed number then. The code is:

while (input != 0) {    
    last_digit = input % 10;
    if (last_digit % 2 != 0) {     
        reversedNum = reversedNum * 10 + last_digit;

    }
    input = input / 10; 
}
🌐
BeginnersBook
beginnersbook.com › 2014 › 01 › java-program-to-reverse-a-number
Java program to reverse a number using for, while and recursion
This remainder becomes the first digit of reversenum, we are repeating this step again and again until the given number become zero and all the digits are appended in the reversenum. At the end of the loop the variable reversenum contains the reverse number and the program prints the value of this variable as output. import java.util.Scanner; class ReverseNumberWhile { public static void main(String args[]) { int num=0; int reversenum =0; System.out.println("Input your number and press enter: "); //This statement will capture the user input Scanner in = new Scanner(System.in); //Captured input would be stored in number num num = in.nextInt(); //While Loop: Logic to find out the reverse number while( num != 0 ) { reversenum = reversenum * 10; reversenum = reversenum + num; num = num/10; } System.out.println("Reverse of input number is: "+reversenum); } }
Find elsewhere
🌐
Medium
medium.com › @salmanshaik3y › reversing-a-number-using-loops-in-java-java-basic-programs-for-interview-084d232d792f
Reversing a Number Using Loops in Java — Java Basic Programs for interview | by SalmanTests | Medium
June 14, 2024 - Output the reversed number: Once the loop completes, reversed contains the original number num in reverse order. ... In the first iteration: num % 10 gives 3. reversed becomes 3. In the second iteration: num % 10 gives 2. reversed becomes 32. In the third iteration: num % 10 gives 1. reversed becomes 321. Thus, reversed now holds 321, which is the reverse of 123. This method is straightforward and uses a loop to reverse the digits of num, making it suitable for beginners learning Java programming.
🌐
Medium
medium.com › @bhagyashree25.sahu › reversing-a-number-using-mathematical-operations-in-java-adbaab18e611
Reversing a Number Using Mathematical Operations in Java | by Bhagyashree Sahu | Medium
June 14, 2024 - It involves breaking down the number into its individual digits and reconstructing it in reverse order. Java provides a straightforward way to accomplish this using basic arithmetic operations like modulo (%) and division (/).
🌐
PREP INSTA
prepinsta.com › home › dsa in java › reverse a given number in java
Reverse a Number in Java | PrepInsta
September 28, 2022 - In this method we’ll use recursion to break down the input integer number and reassemble them in reverse order. We’ll use the modulo and divide operators to break down the number and rearrange them. Let’s implement the logic in Java Language.
🌐
Hero Vired
herovired.com › learning-hub › topics › how-to-reverse-a-number-in-java
How to Reverse a Number in Java: Step-by-Step Guide
Initialise: Start with the number you want to reverse and a variable reverse set to 0. Loop: Use a while loop to iterate as long as the number is not 0. Extract Last Digit: Use number % 10 to get the last digit.
🌐
Medium
rameshfadatare.medium.com › java-program-to-reverse-a-number-bae53d94c2ad
Java Program to Reverse a Number. This guide will show you how to create… | by Ramesh Fadatare | Medium
December 13, 2024 - Reversing: The while loop extracts the last digit of the number using the modulus operator (%). It then constructs the reversed number by appending each digit to the new number. The original number is divided by 10 to remove the last digit.
🌐
Scalable Human Blog
scalablehuman.com › 2023 › 06 › 01 › turn-it-around-reversing-a-number-in-java
Turn It Around: Reversing a Number in Java – Scalable Human Blog
March 9, 2025 - In this blog post, we’ve seen how to reverse a number in Java. By using a while loop, the modulo operation, and integer division, we’ve crafted a simple yet efficient method to turn a number around. It’s another example of how basic mathematics can solve problems in programming.
🌐
Tutorial Gateway
tutorialgateway.org › reverse-a-number-in-java
Java Program to Reverse a Number
April 1, 2025 - From the above Java screenshot, the User Entered value: Number= 9875 and Reverse= 0. ... 2nd Iteration: From the first iteration, the values of both Number and Reverse have changed as Number= 987 and Reverse= 5. ... Here, For the next iteration, Number = 0. So, the while loop condition will fail. This program allows the user to enter any positive integer, and then it will reverse a number using For Loop.
🌐
PrepBytes
prepbytes.com › home › java › java program to reverse a number
Java Program to Reverse a Number
May 16, 2023 - Both approaches have a time complexity of O(log10N), where N is the input number, and a space complexity of O(1) for the iterative approach and O(log10N) for the recursive approach. Understanding these techniques allows one to effectively reverse a number in Java, depending on the needs of their program. Q1. What is the math function to reverse a number in Java?
🌐
Scaler
scaler.com › home › topics › program to reverse a number in java
Program to Reverse a Number in Java - Scaler Topics
September 22, 2023 - To reverse number in Java involves swapping of digit at the first with the digit at the last and, the second digit with the second last until we finish swapping the numbers in the middle.
🌐
Sanfoundry
sanfoundry.com › java-program-reverse-given-number
Reverse a Number in Java - Sanfoundry
March 9, 2023 - To reverse a number, we are using while loop and some arithmetic operations. The idea is to extract each digit of the number one by one, starting from the last digit, and concatenate them in reverse order.
🌐
Technogeeks CS
technogeekscs.com › reverse-a-number-in-java
How To Reverse A Number In Java: A Step-by-Step Guide
March 5, 2026 - Reversing a number in Java is a ... reverse the number by following the process. Convert the number into a string; reverse that string; and convert it back into an integer....
🌐
Blogger
javarevisited.blogspot.com › 2012 › 04 › java-program-to-reverse-number-example.html
Java Program to Reverse an Integer Number - Example tutorial
In last couple of Java programming tutorial, we have seen some basic programming exercises like how to reverse a string in Java using recursion and how to check if a number is prime or not, while in this Java tutorial we will see a simple example of Java program to reverse number by just using basic Java operators like division operator(/) and remainder operator(%).