Drill

ProblemsPython › warmup

Sum of digits

easywarmupPython

A checksum routine needs the digits of a number added together.

digit_sum(amount: int) → int

Solve it in the editor →

Where you start

def digit_sum(amount: int) -> int:
    

Worked examples

CallResult
digit_sum(0)0
digit_sum(7)7
digit_sum(1234)10
digit_sum(-204)6

Hint

Take the absolute value first, then peel digits off with % 10 and / 10.

Reference solution in Python
def digit_sum(amount: int) -> int:
    x = abs(amount)
    total = 0
    while x > 0:
        total += x % 10
        x //= 10
    return total

The same problem in another language

More warmup problems in Python