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 number
2num = int(input("Enter a number (1-9999): "))
3
4# Calculate sum and product of digits
5digit_sum = 0
6digit_product = 1
7temp = num
8
9while temp > 0:
10 digit = temp % 10
11 digit_sum += digit
12 digit_product *= digit
13 temp //= 10
14
15# Compare
16if 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