Saturday, August 19, 2017

Reverse number example in Java?

Reversing a number , means if the given number is "1011" then reverse number will be "1101".
Here is the example.

public class reverseNum {

  public static void main(String[] args) {
         int number=1011;
         int reverseNumber = reverse(number);
         System.out.println(reverseNumber);  
 }

  private static int reverse(int number){
                int reverse = 0;
                int sum=0;
                while(number != 0){
                      reverse = reverse*10 + number%10; 
                      number = number/10;
                }              
                return reverse;
         }
}

No comments: