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 setfocusable in java?
What is certificate authority?
What is the concept of connection pooling?
What is the name of java compiler?
Is java more difficult than c++?
What does java awt stand for?
Is eclipse written in java?
What is meant by applet in java?
What is Connector architecture?
Why tomcat is used in java?
What is j2ee server?
What is enterprise information system resource?
What is java advanced?
Is java is a technology?
What is a java application client?