# How to reverse an integer in Java?

### Author Topic: How to reverse an integer in Java?  (Read 3485 times)

#### sonakshibhathi

• Semi-Newbie
• Thank You 0
• Posts: 10
• Karma: 0
• Gender:
##### How to reverse an integer in Java?
« on: 08-01-2017, 03:53:42 »
Hi friends,
I would like to know,
How to reverse an integer in Java?

#### greatshivam

• Newbie
• Thank You 0
• Posts: 5
• Karma: 0
##### Re: How to reverse an integer in Java?
« Reply #1 on: 02-24-2020, 05:49:14 »
Example-
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--
Original: 239
Reversed: 932
In the above program, we have the following int value, which we will reverse.

int i = 239;
Now, loop through until the value is 0. Find the remainder and perform the following operations to get the reverse of the given integer 239.

while(i != 0) {
int digit = i % 10;
rev = rev * 10 + digit;
i /= 10;
}

### Related Topics

Subject / Started by Replies Last post
19 Replies
9013 Views
07-08-2015, 04:18:14
by Sunainapaul11
37 Replies
8747 Views
04-29-2016, 01:00:10
by MaryGreen
20 Replies
7204 Views
06-18-2014, 05:46:37
by jayanta1
12 Replies
5294 Views
02-29-2016, 03:42:28
by fix.97
3 Replies
8810 Views
08-19-2015, 01:48:54
by SanviMalhotra