Answer the question
In order to leave comments, you need to log in
The sum of all digits of an integer array?
Please tell me how to do it:
For example, if an array [12, 104, 81] is given, then the sum of all its digits will be equal to 1 + 2 + 1 + 0 + 4 + 8 + 1 = 17.
Answer the question
In order to leave comments, you need to log in
int[] arr = {12, 104, 81};
int sum = 0;
for (int i = 0; i < arr.length; i++) {
int temp = arr[i];
while (temp > 0) {
sum += temp % 10;
temp = temp / 10;
}
}
System.out.println(sum);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question