% 10
Last digit
Remainder is the current digit.
Digit sum means adding every digit of a number: 12345 → 1+2+3+4+5 = 15. Extract digits with % 10 and // 10, use abs() for negatives, and stop when the value becomes 0. This tutorial covers a live preview, worked Python examples, edge cases, and complexity.
Last digit
Remainder is the current digit.
Drop digit
Floor-divide to peel the next one.
Negatives
Sign usually does not count.
Edge case
Digit sum of zero is zero.
Try 12345
See digits and the sum.
Bonus fact
Digit sum tests divisibility.
Sum of digits is the classic number-manipulation warm-up: peel digits from right to left with % 10 and // 10, adding each one to a running total. For 12345 you add 5, then 4, then 3, then 2, then 1 — total 15.
Interviews expect abs() for negatives and a clear loop that stops when the remaining value is 0. The same helpers power Armstrong checks, strong numbers, and digital-root / condense problems.
It teaches base-10 digit extraction — the building block for almost every digit-based interview problem.
Extract and drop digits.
total starts at 0.
Ignore the sign.
One step per digit.
In short: total += n % 10, then n //= 10, until n is 0.
Given an integer n, return the sum of its decimal digits (sign ignored).
# 12345 -> 1+2+3+4+5 = 15
# -802 -> 8+0+2 = 10
# 0 -> 0 | Item | Type | Description |
|---|---|---|
n | int | Any integer (negatives OK). |
| Return | int | Sum of decimal digits. |
total | int | Running digit accumulator. |
function digit_sum(n):
n = abs(n)
total = 0
while n > 0:
total += n % 10
n //= 10
return total | Method | Idea | Notes |
|---|---|---|
| Modulo loop | % 10 and // 10 | Interview default |
| String digits | sum(int(c) for c in str(abs(n))) | Short, less “numeric” |
| Condense / digital root | Repeat until one digit | Next tutorial step |
| Goal | Pattern |
|---|---|
| Ignore sign | n = abs(n) |
| Last digit | digit = n % 10 |
| Add | total += digit |
| Drop digit | n //= 10 |
| Stop | while n != 0: (or n > 0) |
| String style | sum(int(c) for c in str(abs(n))) |
Same digits — different packaging.
n % 10Clearest interview answer
str(n)Short Python one-liner
print eachShows peel order
repeatUntil one digit left
Reach for digit sum whenever you need base-10 peeling.
First number-manipulation drill.
Same digit loop, different ops.
Digit sum mirrors the test.
Repeat until one digit remains.
This tutorial focuses on integers.
Key benefit: one tiny loop — % 10 / // 10 — that unlocks a whole family of digit problems.
Enter any integer (e.g. 12345 or -802) and see the digits plus their sum.
Three complete Python programs — sum digits of 12345, read user input, and trace peeling for a negative value. Click View Output to reveal sample console results.
abs, then peel digits with modulo and floor division.
Classic example: 12345 → 15.
def digit_sum(number: int) -> int:
number = abs(number)
total = 0
while number != 0:
total += number % 10
number //= 10
return total
number = 12345
print(f"The sum of digits of {number} is: {digit_sum(number)}") Digits are taken from the right: 5, 4, 3, 2, 1. The accumulator climbs to 15, then the loop stops when the remaining value is 0.
Same helper, value comes from the user.
Reads one integer and prints the digit sum. Negatives work because of abs().
def digit_sum(number: int) -> int:
number = abs(number)
total = 0
while number != 0:
total += number % 10
number //= 10
return total
number = int(input("Enter a number: ").strip())
print(f"Sum of digits: {digit_sum(number)}") Input and summing stay separate: parse once, then reuse digit_sum. Try -802 to confirm the sign is ignored.
Print each extracted digit so you can see the right-to-left order and the abs step.
n = -802
x = abs(n)
total = 0
print(f"Starting from abs({n}) = {x}")
while x != 0:
digit = x % 10
total += digit
print(f" take {digit}, remaining {x // 10}, total={total}")
x //= 10
print(f"Digit sum: {total}") Zero digits still get extracted and add nothing. The sign disappears before the loop, so negatives and positives share the same path.
Drop the sign before peeling.
Take the last digit.
Accumulate and peel.
When n becomes 0, you are done.
Watch digits peel from the right while the accumulator climbs to 15.
| n before | n % 10 | total after | n after // 10 |
|---|---|---|---|
12345 | 5 | 5 | 1234 |
1234 | 4 | 9 | 123 |
123 | 3 | 12 | 12 |
12 | 2 | 14 | 1 |
1 | 1 | 15 | 0 |
Same steps for -802 after abs: 2, then 0, then 8 → total 10.
Where digit sums show up beyond the interview prompt.
Modulo digit extraction drill.
Example: digit_sum(12345).
Rules for 3 and 9.
Example: Did you know fact.
Same peel, different math.
Example: related links.
Lightweight digit totals.
Example: validation helpers.
Repeat until one digit.
Example: condense tutorial.
Continue the interview chain.
Example: related CTA.
Pro Tip: open with “% 10 takes the digit, // 10 drops it” before coding.
Why the modulo loop is the right first approach.
No string conversion required.
Dry-run 12345 on paper in seconds.
O(1) extra space beyond the input.
Powers Armstrong, strong, condense, and more.
Pro Tip: mention the string one-liner only after you show the numeric loop.
Small habits that keep digit-sum solutions interview-ready.
Avoid signed modulo surprises.
Same accumulator idea as array sum.
Or note the loop never runs and returns 0.
Digits come off the end first.
Know the difference: one pass vs repeat.
Pro Tip: sanity-check 0, 12345, and -802 — if those three work, you are solid.
Mistakes that commonly break digit-sum programs.
Negative remainders confuse beginners.
→ Call abs(n) first.
Floats break the peel loop.
→ Always floor-divide with //.
Forgetting to update n.
→ Always n //= 10 inside the loop.
Stopping at one digit vs one pass.
→ Digit sum is a single pass.
Returning wrong values for n = 0.
→ Digit sum of 0 is 0.
Handle these before claiming the digit sum is complete.
Sum of digits of 0 is 0.
Most tasks ignore the sign.
7 → 7.
0 adds nothing but must be extracted.
Each loop handles one digit.
1+2+3+4+5 = 15.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
abs().Quick Takeaway: total += n % 10; n //= 10; repeat until done.
| Approach | Time | Extra space |
|---|---|---|
| Digit loop | O(d) | O(1) |
| String conversion | O(d) | O(d) for the string |
| Condense (repeat) | O(d) overall | O(1) |
d is the number of digits — about floor(log10 |n|) + 1 for n ≠ 0.
Digit sum peels a number with % 10 and // 10, adding each digit to a running total. Use abs() for negatives, and remember that 0 sums to 0.
Practice the three examples above, then continue to condensing a number.
total += n % 10; n //= 10.
Master the peel loop that powers later digit problems.
n % 10
Extractn //= 10
Loopignore sign
Guardsum is 0
EdgeO(d)
AnalysisDigit sum helps with divisibility tests: a number is divisible by 3 (or 9) exactly when its digit sum is divisible by 3 (or 9).
Learn how to repeatedly sum digits until a single digit remains.
9 people found this page helpful