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 |
What is difference between j2ee and java?
What is the difference between sendRedirect() and forward()? in what situations do we have to use send redirect() instead of forward().
13 Answers Honeywell, Mind Tree, Samsung, Wipro,
How can I be good in java?
Why java is oop language?
What is ear file?
What is distributed application?
Whats the O/p of the below code snippet ? And explain how does it imply the concept of call-by-value/call-by reference. (PS : Pls ignore syntax errors) public class One { public oneA(){ sop ("Into One--");} } public class Two extends One{ public twoT(){ sop ("Into Two--"); } } public class Home { One a; Two t; public static void main(argv[]) { sop ("In Home--"); sop(a.oneA()); sop(t.oneA()); sop(a.twoT()); sop(t.twoT()); } }
Sapient Interview Questions
Is python more secure than java?
Is core java and j2se same?
What is jaxr provider?
What is japplet in java?