Reverse the number using command line argument.
How to reverse the number you enter and find sum of this number . Here is simple
java code for beginners to reverse the number .
In this program we pass argument through command you want to reverse then
using modulus operator we can reverse this number . Each time number divided by 10 .
Here r is remainder of n.
public class ReverseNumber {
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);
}
}
When we run this code using argument output will be .
entered num is: 2345
sum of digits is: 14
reverse of the num is: 5432
Reverse the number using command line argument.
Reviewed by Mukesh Jha
on
10:54 AM
Rating:
No comments:
Add your comment