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 is setfocusable in java?

577


What is certificate authority?

570


What is the concept of connection pooling?

548


What is the name of java compiler?

486


Is java more difficult than c++?

478






What does java awt stand for?

524


Is eclipse written in java?

547


What is meant by applet in java?

472


What is Connector architecture?

513


Why tomcat is used in java?

492


What is j2ee server?

553


What is enterprise information system resource?

548


What is java advanced?

503


Is java is a technology?

523


What is a java application client?

529