Tuesday, 16 June 2015

Reverse Number using Java

This program prints reverse of a number i.e. if the input is 4321 then output will be 1234.

/*
  Reverse Number using Java
 
*/
package in.blogspot.java2bigdata 
public class ReverseNumberDemo {
 
        public static void main(String[] args) {
               
                int number = 4321;
                int reversedNumber = 0;
                int temp = 0;
               
                while(number > 0){
                       
                        //use modulus operator to strip off the last digit
                        temp = number%10;
                       
                        //create the reversed number
                        reversedNumber = reversedNumber * 10 + temp;
                        number = number/10;
                         
                }
               
                //output the reversed number
                System.out.println("Reversed Number is: " + reversedNumber);
        }
}
 
/*
Output of this Number Reverse program would be
Reversed Number is: 1234
*/

1 comment:

  1. Replica Watches uk, combining elegant style and cutting-edge technology, a variety of styles of replica audemars piguet watches, the pointer walks between your exclusive taste style.

    ReplyDelete