Write a program for Sum of the digits for a given number
Ex: suppose user entered Number is 1214,
It should calculate the above number like 1+2+1+4 = 8
Answer Posted / vishal
public void getDigitSum(int num){
int remainder=0;
int sum=0;
String strResult="";
int counter=0;
while(num>0){
remainder=num%10;
sum=sum+remainder;
counter++;
if(counter==1){
strResult=String.valueOf(remainder);
}
else{
strResult=String.valueOf(remainder)+"+"+strResult;
}
num=num/10;
}
System.out.println(strResult+"="+sum);
}
| Is This Answer Correct ? | 11 Yes | 0 No |
Post New Answer View All Answers
Is java is a technology?
What is server in j2ee?
How do you make a class immutable in java?
What is java servlet container?
Describe the ear, war, and jar.
How do you run a java program?
What does java ee stand for?
What is java ee used for?
What is the use of method save()?
What is basic authentication?
What is javamail?
What does j2ee stand for?
What is ejb module?
What does the application client module contain?
How do I become an expert in java?