Numbers with Even Digit Sum

Print numbers 1-100 whose digit sum is even.

AdvancedPhase 2 - Level 5: Logical Loop CombinationsExample 1 of 12
numbers-with-even-digit-sum.py
1# Print numbers with even digit sum
2print("Numbers 1-100 with even digit sum:")
3for num in range(1, 101):
4 # Calculate digit sum
5 digit_sum = 0
6 temp = num
7 while temp > 0:
8 digit_sum += temp % 10
9 temp //= 10
10
11 # Check if sum is even
12 if digit_sum % 2 == 0:
13 print(num, end=" ")
14print()

Output

Numbers 1-100 with even digit sum:
2 4 6 8 11 13 15 17 19 20 ...

What's going on

Calculate digit sum and check if even.

Key Concepts:

Extract digits and sum
Check if sum % 2 == 0
Filter numbers meeting condition