English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
To reverse an integer in Java, try the following code-
import java.lang.*; public class Demo { public static void main(String[] args) { int i = 239, rev = 0; System.out.println("Original: " + i); while(i != 0) { int digit = i % 10; rev = rev * 10 + digit; i /= 10; {} System.out.println("Reversed: " + rev); {} {}
Output Result
Original: 239 Reversed: 932
In the above program, we have the following int values, which we reverse.
int i = 239;
Now, loop through until the value is 0. Find the remainder and perform the following operations to obtain the integer239The opposite value.
while(i != 0) { int digit = i % 10; rev = rev * 10 + digit; i /= 10; {}