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
Why java is not 100% object oriented language?
What does enterprise javabeans module contain?
What is container-managed transaction?
Why java is a platform?
Explain jta, jndi, and jms.
What is difference between javac and java?
i want test pattern of zylog company and some examples also
What are the four types of J2EE modules?
Is eclipse written in java?
Are java primitives objects?
On which platform j2ee is based?
What is the mvc on struts?
Why is java considered dynamic?
What are the component of java?
What is j2ee application?