Question:
How to reverse a number in Java?
Code:
class ReverseNumber { public static void main(String args[]) { int num=123456789; int r=0; int s=0; System.out.println("Number: " + num); while(num>0) { r=num%10; s=(s*10)+r; num=num/10; } System.out.println("Reverse: " + s); } }
Output:
$ java ReverseNumber Number: 123456789 Reverse: 987654321