Reverse of number





 Here  is  simple  java example  to  reverse  the number .  This  example  is asked  in interview
 many time  for junior level developers . So  go through this code  for  your interview preparation.



class Revrsenumber
 {
public static void main(String args[])

  {
 int n=Integer.parseInt(args[0]); 
 System.out.println("entered num is: "+n); 

 int r,sum=0,rev=0;
  while(n>0) {
   r=n%10; n=n/10; 
   
   rev=(rev*10)+r; 
   
   sum+=r;
}

System.out.println("sum of digits is: "+sum); 

System.out.println("reverse of the num is: "+rev);
} 


}

 When you run this code with run configuration  arguments . Output will like 
 this  a input number 

entered num is: 45678
sum of digits is: 30
reverse of the num is: 87654

Reverse of number Reverse of number Reviewed by Mukesh Jha on 9:59 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.