Android & iPhone apps are coming soon. Log in for a plan and reminders made for you →

Coding basics · 12 of 20

Sum of digits

Return the sum of the digits of a non-negative integer n.

Jump to the editor ↓

What you need

n % 10 is the last digit and n // 10 is everything before it. Together they take a number apart from the right.

n = 472
n % 10    # 2
n // 10   # 47

Examples

Input n = 1234

Output 10

1 + 2 + 3 + 4

Input n = 0

Output 0

Your code

Tab indents · ⌘/Ctrl+Enter runs · Esc leaves the editor

Stuck?