# How to reverse an integer in Java?

### Author Topic: How to reverse an integer in Java?  (Read 4236 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;
}

#### brookeroberts7

• Full Member
• Thank You 2
• Posts: 171
• Karma: 0
##### Re: How to reverse an integer in Java?
« Reply #2 on: 07-06-2020, 01:17:30 »
class ReverseNumber. { public static void main(String args[]) { int n, reverse = 0;
System. out. println("Enter an integer to reverse"); Scanner in = new Scanner(System. in); n = in. nextInt();
while(n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10; }

#### Lishmalinyjames

• Hero Member
• Thank You 2
• Posts: 800
• Karma: 0
##### Re: How to reverse an integer in Java?
« Reply #3 on: 01-27-2021, 07:12:28 »
class ReverseNumber. { public static void main(String args[]) { int n, reverse = 0;
System. out. println("Enter an integer to reverse"); Scanner in = new Scanner(System. in); n = in. nextInt();
while(n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10; }

### Related Topics

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