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


Please Help Members By Posting Answers For Below Questions

What do you mean by j2ee module?

723


Which java technology is in demand?

660


What is j2ee application model?

641


What is j2ee architecture?

643


Why java is not 100% object oriented language?

693


to update a batch in a table in jdbc which method of statement object is used

1847


What are the different types of events in java?

610


What is enterprise information system?

719


Is tomcat a java ee?

653


What is a j2ee component?

672


What is client-certificate authentication?

749


Define hashtable in j2ee?

712


Which framework is best for java?

677


What are the j2ee technologies?

656


Difference between save and saveorupdate?

679