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?
Why is java called object oriented?
Which framework is best for java?
Can I run seam in a j2ee environment?
What is external entity?
what is direct link to download swing ebook,applet,ejb,core java
How java is used in web applications?
What spring is in related to j2ee?
What is declaration?
What is advance java?
What is the concept of connection pooling?
What is j2ee api?
What type of code is java?
What is java api for xml registries (jaxr)?
What is a component in java?