Get Sum Of Digit Entered And Reverse the Number






This  program   is  good  example  of   getting  number through  command line  and 
 getting    the number in reverse order .This  simple  mathematical trick  to get the 
 result . Please  try torun  it  by yourself .

 public class ReverseSum{

public static void main(String args[]) {
int n=0;  
String s=args[0];
    n=Integer.parseInt(s); 
 
   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);
    } 
}

************Program Output ****************

 we  have enter digit 6785 through command line 

entered num is: 6785
sum of digits is: 26
reverse of the num is: 5876
Get Sum Of Digit Entered And Reverse the Number Get Sum Of Digit Entered And Reverse  the Number Reviewed by Mukesh Jha on 10:20 AM Rating: 5

No comments:

Add your comment

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