October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Find the Sum of Even Digits in a Number in Python

Extract each decimal digit with modulo and integer division, then add only digits divisible by two. Includes a string-based option and edge-case guidance.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How digit extraction works

  • number % 10 gives the remainder after division by ten, which is the final decimal digit for a nonnegative integer.
  • digit % 2 == 0 checks whether that digit is divisible by two. This tests the digit itself, not whether the whole number is even.
  • number //= 10 uses integer floor division to drop the final digit.
  • total accumulates 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.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use 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.Support on Ko-Fi

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.