Sum of Digits of a Number in Python
The standard way to take a number apart digit by digit — the same technique is used for reverse, palindrome and Armstrong programs.
Quick answer
Take the last digit with n % 10, add it to a total, and remove it with n //= 10; repeat while n > 0. For 1234 that is 4 + 3 + 2 + 1 = 10. In Python, 1234 % 10 is 4 and 1234 // 10 is 123. The shortest way is sum(map(int, str(n))).
The question
Read a positive integer and print the sum of its digits. Use a while loop with % 10 and // 10.
The code
program.py
n = int(input())
total = 0
while n > 0:
total += n % 10
n //= 10
print("Sum of digits =", total)Input
72050
Output
Sum of digits = 14
How it works
- 1n % 10 gives the last digit (1234 % 10 = 4).
- 2Add that digit to total.
- 3n //= 10 removes the last digit (1234 becomes 123).
- 4Repeat while n is greater than 0; when every digit is used, n becomes 0 and the loop ends.
Dry run for 1234
| n | n % 10 | total | n // 10 |
|---|---|---|---|
| 1234 | 4 | 4 | 123 |
| 123 | 3 | 7 | 12 |
| 12 | 2 | 9 | 1 |
| 1 | 1 | 10 | 0 |
n becomes 0 after four passes, so the loop stops and the sum of digits is 10.
Common mistakes
- Using / instead of // — n becomes 123.4 and the loop never works properly.
- Forgetting to change n inside the loop, which makes the loop run forever.
Sum of digits using a for loop and a string
Leave the input as text. A for loop over a string gives one character at a time, and int(ch) turns each one into a digit.
program.py
n = input().strip()
total = 0
for ch in n:
total += int(ch)
print("Sum of digits =", total)Input
1234
Output
Sum of digits = 10
Sum of digits in one line using sum() and map()
map(int, text) turns every character into a digit and sum() adds them. Handy outside the exam; when the question says "use a while loop", write the loop.
Input
1234
Output
Sum of digits = 10
Sum of digits using recursion
The last digit plus the sum of digits of the rest: digit_sum(1234) = 4 + digit_sum(123). The recursion stops when n becomes 0.
program.py
def digit_sum(n):
if n == 0:
return 0
return n % 10 + digit_sum(n // 10)
n = int(input())
print("Sum of digits =", digit_sum(n))Input
1234
Output
Sum of digits = 10
Questions students ask
- What is 1234 // 10 in Python?
- 123. The // operator is floor division: it divides and throws away the remainder, so the last digit disappears. 1234 / 10 with a single slash gives 123.4.
- What is 1234 % 10 in Python?
- 4. The % operator gives the remainder, and the remainder after dividing by 10 is always the last digit.
- What does n //= 10 mean in Python?
- It is short for n = n // 10 — divide n by 10, drop the remainder and store the result back in n. In a digit loop it removes the last digit on every pass until n becomes 0.
Now solve a similar question yourself
A new Python question every time, checked instantly.
Preparing for NIELIT O Level?
This program is the kind asked in the M3 (Python) theory paper and the PR-3 practical exam.
Want to build websites and apps with AI tools instead of writing every line yourself? See our AI Digital Pro course in Kaithal.
