Digit Sum vs Product
Take an integer (1-9999) and check if the sum of its digits is greater than the product of its digits.
AdvancedPhase 1 - Level 5: Creative / Tricky Logical ScenariosExample 7 of 10
digit-sum-vs-product.py
1# Take number2num = int(input("Enter a number (1-9999): "))34# Calculate sum and product of digits5digit_sum = 06digit_product = 17temp = num89while temp > 0:10 digit = temp % 1011 digit_sum += digit12 digit_product *= digit13 temp //= 101415# Compare16if digit_sum > digit_product:17 print("Sum is greater than product")18elif digit_sum < digit_product:19 print("Product is greater than sum")20else:21 print("Sum equals product")
Output
Enter a number (1-9999): 123 Sum is greater than product Enter a number (1-9999): 234 Product is greater than sum
What's going on
Extract digits and calculate sum and product.
Key Concepts:
Extract digits using modulo and division
Accumulate sum and product
Compare the two values
🔗Related Content
- 📖
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- 📝
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- 💻
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum