← Control Flow

Digit Sum

hard

Read a single non-negative integer n and print the sum of its decimal digits, in the form Digit sum: 16.

You’re not allowed to treat the number as a string here – pull digits off the end of the number using % 10 (remainder when dividing by 10 gives the last digit) and / 10 (integer division drops that digit), repeating until nothing is left. This pattern – peel off the low digit, shrink the number, repeat – shows up constantly: reversing a number, converting bases, checking for palindromes. Trace n = 493 by hand before you code it: what are n, n % 10, and n / 10 after each iteration?

Your solution

"Run" uses the sample stdin ("493"). "Submit" checks your code against all 4 test cases.

Test results

Submit your solution to run it against all test cases.

Hints

    Reference solution