Assignment: "Write a program SumOfDigits that reads an integer
greater than 0 and smaller than 1000. Add all the digits in the integer.
For example, if an integer is 932, the sum of all its digits is 14.
Hint: Take use of division (/) and remainder (%) operators."
/* Simple Java Program To Find the Sum of digits of number */
class SumDigit
{
public static void main(String args[])
{
int sum, i,a,d;
a = Integer.parseInt(args[0]);
sum = 0;
for(i=1;i< =10;i++)
{d = a%10;
a = a/10;
sum=sum + d;}
System.out.println("Sum of Digit :"+sum);
}
}
Obviously using "For" in the code is incorrect, but this is one of the problems of my first assignment, I need help figuring out how to correct it.
Question
Alladaskill17
Assignment: "Write a program SumOfDigits that reads an integer
greater than 0 and smaller than 1000. Add all the digits in the integer.
For example, if an integer is 932, the sum of all its digits is 14.
Hint: Take use of division (/) and remainder (%) operators."
/* Simple Java Program To Find the Sum of digits of number */ class SumDigit { public static void main(String args[]) { int sum, i,a,d; a = Integer.parseInt(args[0]); sum = 0; for(i=1;i< =10;i++) {d = a%10; a = a/10; sum=sum + d;} System.out.println("Sum of Digit :"+sum); } }Obviously using "For" in the code is incorrect, but this is one of the problems of my first assignment, I need help figuring out how to correct it.
Link to comment
Share on other sites
14 answers to this question
Recommended Posts