Use % 10 to take the last digit, add it if it is even, then use // 10 to remove it. For example, the even digits in 48215 are 4, 8, and 2, so their sum is 14.
Use a loop to sum even digits
This function processes a nonnegative copy of the integer one digit at a time. It also treats a negative number as its magnitude, ignoring the minus sign.
As an Amazon Associate I earn from qualifying purchases.
def sum_even_digits(number: int) -> int:
number = abs(number)
total = 0
while number > 0:
digit = number % 10
if digit % 2 == 0:
total += digit
number //= 10
return total
print(sum_even_digits(48215)) # 14
The result is the same whether the digits are visited from right to left or left to right, because addition does not depend on order.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
How digit extraction works
number % 10gives the remainder after division by ten, which is the final decimal digit for a nonnegative integer.digit % 2 == 0checks whether that digit is divisible by two. This tests the digit itself, not whether the whole number is even.number //= 10uses integer floor division to drop the final digit.totalaccumulates only the even digits.
The Python Software Foundation’s Python tutorial explains that // is floor division and % calculates a remainder. Use //, not /, when removing digits: / produces a floating-point result.
#1 Best Overall
Handle zero, negatives, and other inputs
Zero and numbers with no even digits
sum_even_digits(0) returns 0: the loop does not run, so the initial total is returned. A number such as 1357, which has no even digits, also returns 0.
Negative integers
The call to abs makes the function sum the digits of the number’s magnitude. For example, sum_even_digits(-48215) returns 14. If your application should treat negative numbers differently, define that rule explicitly instead of silently removing the sign.
Rank #2
Input type and leading zeros
The arithmetic function expects an integer. Integers do not preserve leading zeros; if the original text matters, such as an identifier, process it as a string instead. Zero digits contribute nothing to the sum.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitchesUse a string approach for textual input
If the input already arrives as text, this version accepts an optional leading plus or minus sign and converts each character to an integer before checking it:
def sum_even_digits_text(text: str) -> int:
return sum(int(ch) for ch in text.lstrip("+-") if int(ch) % 2 == 0)
print(sum_even_digits_text("48215")) # 14
print(sum_even_digits_text("-48215")) # 14
This compact form assumes the remaining characters are decimal digits. For user-entered text, validate it first and decide how to handle empty strings, repeated signs, spaces, or other characters.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Choose the approach that fits
| Approach | Best fit | What to keep in mind |
|---|---|---|
| Arithmetic loop | An integer input, especially when learning digit extraction | Makes the roles of % 10, // 10, and the accumulator explicit. |
| String iteration | Input that is already textual | Convert digit characters to integers before testing parity; validate text if it is user-facing. |
Generator expression with sum |
A concise version once iteration and parity checks are familiar | Keep the accumulator named total, not sum, so Python’s built-in sum remains available. |
Both approaches take time proportional to the number of digits. There is no need to use recursion for this task; it adds complexity without changing the result.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Free tools Windows power users keep installed
One-click scans. No signup required.




