How to reverse an integer in Java?

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

Offline brookeroberts7

  • Trade Count: (0)
  • Jr. Member
  • **
  • Thank You 0
  • Posts: 85
  • 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; }
<a href="https://www.assignment-homework.com/services/homework-writing-services/">homework writing service</a>

 

Related Topics

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