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
Answers were Sorted based on User's Feedback
Answer / 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 |
Answer / gunreddy
Scanner scan = new Scanner(System.in);
int in = scan.nextInt();
String s = in + "";
int sum = 0;
int div = 10;
int di = 1;
for (int i = 0; i < s.length(); i++) {
int rem = in % div;
rem = rem / di;
sum += rem;
div *= 10;
di *= 10;
}
System.out.println("sum " + sum);
| Is This Answer Correct ? | 3 Yes | 0 No |
i have to create an application with session.during that session a timer will be started & show the time in second upto the session expired.So that i can know how many time i in that session. Hint:-i m hinted that it will coded with javascript
What is declarative security ?
Which package is imported by default in java?
What are the components of awt in java?
Is a relationship java?
How java is used in web applications?
Hi friends, can u explain Transparent Persistence in hibernate with sample code.
what is external varibles in jsp?
How many types of component events are in java?
What is the best java application server?
How do I become a java developer?
What is comment?