How to reverse an integer in Java?

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

Offline sonakshibhathiTopic starter

  • Trade Count: (0)
  • Semi-Newbie
  • *
  • Thank You 0
  • Posts: 10
  • Karma: 0
  • Gender: Female
    • Best Dental Implant Clinic in Jaipur
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?


Offline greatshivam

  • Trade Count: (0)
  • 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
8962 Views
Last post 07-08-2015, 04:18:14
by Sunainapaul11
37 Replies
8671 Views
Last post 04-29-2016, 01:00:10
by MaryGreen
20 Replies
7147 Views
Last post 06-18-2014, 05:46:37
by jayanta1
12 Replies
5276 Views
Last post 02-29-2016, 03:42:28
by fix.97
3 Replies
8714 Views
Last post 08-19-2015, 01:48:54
by SanviMalhotra