Sum of Digits
Learn how to find the sum of all digits in a number in Python.
Sum of Digits
Problem
Given a number, find the sum of all its digits.
Input: 1234
Output: 10 (1 + 2 + 3 + 4)
Input: 9875
Output: 29 (9 + 8 + 7 + 5)
Input: -456
Output: 15 (4 + 5 + 6)
Input: 0
Output: 0Logic
- If the number is negative, work with the positive version
- Extract each digit one by one
- Add each digit to the total
- Return the total
Flow
Solution 1 — using a loop and math
Extract each digit using % 10 and remove it using // 10. Classic approach — no strings involved.
def sum_of_digits(n):
# handle negative numbers
n = abs(n)
total = 0
while n > 0:
digit = n % 10 # get the last digit
total += digit # add it to total
n = n // 10 # remove the last digit
return total
print(sum_of_digits(1234)) # 10
print(sum_of_digits(9875)) # 29
print(sum_of_digits(-456)) # 15
print(sum_of_digits(0)) # 0Code Execution — Solution 1
Trace through sum_of_digits(1234):
| Loop | n | digit = n % 10 | total += digit | n = n // 10 |
|---|---|---|---|---|
| Start | 1234 | — | 0 | — |
| 1st | 1234 | 4 | 0 + 4 = 4 | 123 |
| 2nd | 123 | 3 | 4 + 3 = 7 | 12 |
| 3rd | 12 | 2 | 7 + 2 = 9 | 1 |
| 4th | 1 | 1 | 9 + 1 = 10 | 0 |
| End | 0 | loop stops |
Result: 10
Now trace sum_of_digits(9875):
| Loop | n | digit = n % 10 | total | n = n // 10 |
|---|---|---|---|---|
| Start | 9875 | — | 0 | — |
| 1st | 9875 | 5 | 5 | 987 |
| 2nd | 987 | 7 | 12 | 98 |
| 3rd | 98 | 8 | 20 | 9 |
| 4th | 9 | 9 | 29 | 0 |
| End | 0 | loop stops |
Result: 29
n % 10 gives the last digit of any number. 1234 % 10 = 4. Then n // 10 removes that last digit. 1234 // 10 = 123. These two operations let you walk through every digit from right to left.
Solution 2 — using string conversion
Convert the number to a string, loop through each character, convert each back to an integer, and add them up.
def sum_of_digits(n):
# handle negative numbers — abs() removes the minus sign
n = abs(n)
total = 0
# str(n) converts number to string — "1234"
# each char is one digit — "1", "2", "3", "4"
for char in str(n):
total += int(char) # convert each character back to int and add
return total
print(sum_of_digits(1234)) # 10
print(sum_of_digits(9875)) # 29
print(sum_of_digits(-456)) # 15
print(sum_of_digits(0)) # 0Code Execution — Solution 2
Trace through sum_of_digits(-456):
abs(-456) → 456
str(456) → "456"
| Loop | char | int(char) | total |
|---|---|---|---|
| Start | — | — | 0 |
| 1st | "4" | 4 | 4 |
| 2nd | "5" | 5 | 9 |
| 3rd | "6" | 6 | 15 |
| Done | 15 |
Solution 3 — using sum() and a generator
The most concise solution. Convert to string, convert each character to int, pass everything to sum().
def sum_of_digits(n):
# abs() handles negatives
# str() converts to string
# int(char) converts each character to integer
# sum() adds them all up
return sum(int(char) for char in str(abs(n)))
print(sum_of_digits(1234)) # 10
print(sum_of_digits(9875)) # 29
print(sum_of_digits(-456)) # 15
print(sum_of_digits(0)) # 0Code Execution — Solution 3
Trace through sum_of_digits(9875):
| Step | Code | Result |
|---|---|---|
| abs | abs(9875) | 9875 |
| str | str(9875) | "9875" |
| generator | int(char) for char in "9875" | 9, 8, 7, 5 |
| sum | sum(9, 8, 7, 5) | 29 |
Solution 4 — using recursion
Call the function on itself — each call strips one digit and adds it to the result of the next call.
def sum_of_digits(n):
# handle negative numbers
n = abs(n)
# base case — single digit number, just return it
if n < 10:
return n
# recursive case:
# n % 10 → last digit
# sum_of_digits(n // 10) → sum of all remaining digits
return (n % 10) + sum_of_digits(n // 10)
print(sum_of_digits(1234)) # 10
print(sum_of_digits(9875)) # 29
print(sum_of_digits(-456)) # 15
print(sum_of_digits(0)) # 0Code Execution — Solution 4
Trace through sum_of_digits(1234):
Each call strips the last digit and passes the rest to the next call:
sum_of_digits(1234)
= (1234 % 10) + sum_of_digits(123)
= 4 + sum_of_digits(123)
= 3 + sum_of_digits(12)
= 2 + sum_of_digits(1)
= 1 ← base case, 1 < 10
= 2 + 1 = 3
= 3 + 3 = 6
= 4 + 6 = 10Result: 10
Recursion breaks the problem into smaller versions of itself. Each call handles one digit and passes the rest to the next call. The base case n < 10 stops the recursion — without it the function would call itself forever.
Which solution to use?
| Solution | How | Best when |
|---|---|---|
| Solution 1 | Loop + % 10 and // 10 | Understanding digit extraction |
| Solution 2 | String conversion + loop | Readable, easy to follow |
| Solution 3 | sum() + generator | Clean one-liner |
| Solution 4 | Recursion | Understanding recursive thinking |
Output
10
29
15
0