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 do you mean by j2ee module?
Which java technology is in demand?
What is j2ee application model?
What is j2ee architecture?
Why java is not 100% object oriented language?
to update a batch in a table in jdbc which method of statement object is used
What are the different types of events in java?
What is enterprise information system?
Is tomcat a java ee?
What is a j2ee component?
What is client-certificate authentication?
Define hashtable in j2ee?
Which framework is best for java?
What are the j2ee technologies?
Difference between save and saveorupdate?