D
D
Dmitry Kozyrev2021-07-03 09:01:28
Java
Dmitry Kozyrev, 2021-07-03 09:01:28

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

1 answer(s)
Y
Yuriy Vorobyov, 2021-07-03
@YuriyVorobyov1333

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 question

Ask a Question

731 491 924 answers to any question