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 / 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 |
Post New Answer View All Answers
What is awt panel java?
What is java advanced?
Is a relationship java?
What is server in j2ee?
What is the concept of connection pooling?
how to run servlet program between two computer through internet ?
What is iso 3166?
What is java 2 platform, enterprise edition (j2ee)?
What is application client?
What is full form of j2ee?
What is jta and jts?
What are struts?
Is core java and j2se same?
What is j2ee and j2se?
What is java api for xml processing (jaxp)?