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
What is java web technologies?
What is repaint in java?
What are the tools in java?
What is entity reference?
Define authorization constraint?
What is webservices in java?
Why is java called technology?
Describe action form?
What are j2ee technologies?
What do you understand by actionform?
What do you mean by j2ee module?
What is the difference between java and core java?
What is j2ee specification?
What is javaserver pages standard tag library (jstl)?
What is source file in java?