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
Is tomcat a java ee?
What is the difference between java and core java?
Who gave the name java?
Explain the four types of container that the j2ee specification defines
Define connection pooling?
What is data?
What is actionform?
What is a java applet?
What are the j2ee client types?
Is downloading java safe?
What is declarative security ?
What is bean factory, have you used xmlbean factory?
What is full form of j2ee?
Why java is oop language?
What is ear file?