Pin It

How to reverse a number in Java ?.



Program to demonstrate how to reverse a number in Java



 
import java.util.Scanner;

public class ReverseNumber {

 public static void main(String[] args) {

  Scanner scanner = new Scanner(System.in); 

  System.out.print("Enter a number to reverse : "); 

  int original = scanner.nextInt();
  int reverse = 0;
  int remainder;

  while(original != 0) { 

   remainder = original % 10; 
   reverse = reverse * 10 + remainder; 
   original = original / 10; 

  }

  System.out.println("Reverse of number is : " + reverse);


 }

}


Output of the program : 

 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot