Sum of Odd & Even Digits Separately

Calculate sum of odd and even digits separately.

AdvancedPhase 2 - Level 5: Logical Loop CombinationsExample 9 of 12
sum-of-odd-even-digits-separately.py
1# Take number
2num = int(input("Enter a number: "))
3
4# Sum odd and even digits
5sum_odd = 0
6sum_even = 0
7temp = abs(num)
8
9while temp > 0:
10 digit = temp % 10
11 if digit % 2 == 0:
12 sum_even += digit
13 else:
14 sum_odd += digit
15 temp //= 10
16
17print(f"Sum of odd digits: {sum_odd}")
18print(f"Sum of even digits: {sum_even}")

Output

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

What's going on

Separate digits into odd and even.

Key Concepts:

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