Sum of Odd & Even Digits Separately

Calculate sum of odd and even digits separately.

Logic BuildingAdvanced
Logic Building
# Take number
num = int(input("Enter a number: "))

# Sum odd and even digits
sum_odd = 0
sum_even = 0
temp = abs(num)

while temp > 0:
    digit = temp % 10
    if digit % 2 == 0:
        sum_even += digit
    else:
        sum_odd += digit
    temp //= 10

print(f"Sum of odd digits: {sum_odd}")
print(f"Sum of even digits: {sum_even}")

Output

Enter a number: 12345
Sum of odd digits: 9
Sum of even digits: 6

Separate digits into odd and even.

Key Concepts:

  • Extract each digit
  • Check if even or odd
  • Add to respective sum