Saturday, 26 April 2014

Ex 6: Write a program in Java to compute the sum of the digits of a given integer. Remember, your integer should not be less than the five digits.

class s02_02
{
public static void main(String arg[])
{
int sum=0;
long n=Long.parseLong(arg[0]);
while(n>0)
{
sum+=n%10;
n/=10;
}
System.out.println("Sum="+sum);
}
}

No comments:

Post a Comment