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 number2num = int(input("Enter a number: "))34# Sum odd and even digits5sum_odd = 06sum_even = 07temp = abs(num)89while temp > 0:10 digit = temp % 1011 if digit % 2 == 0:12 sum_even += digit13 else:14 sum_odd += digit15 temp //= 101617print(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
🔗Related Content
- 📖
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- 📝
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- 💻
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum