Logic Building
# Take number
num = int(input("Enter a number (1-9999): "))
# Calculate sum and product of digits
digit_sum = 0
digit_product = 1
temp = num
while temp > 0:
digit = temp % 10
digit_sum += digit
digit_product *= digit
temp //= 10
# Compare
if digit_sum > digit_product:
print("Sum is greater than product")
elif digit_sum < digit_product:
print("Product is greater than sum")
else:
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
Extract digits and calculate sum and product.
Key Concepts:
- Extract digits using modulo and division
- Accumulate sum and product
- Compare the two values