How to reverse a number in java

Java code to reverse a given number




import java.io.*;

class Test {
          public static void main(String[] args) throws IOException {

              int num,sum=0,r;
              System.out.println("Enter a number");
              BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
              num=Integer.parseInt(br.readLine());

              while(num!=0){
                   r=num%10;
                   sum=sum*10+r;
                   num=num/10;
              }

              System.out.println("Reverse number="+sum);
          }
}

No comments: