Product of Digits

Calculate product of digits of a number.

IntermediatePhase 2 - Level 2: Number-based Looping LogicExample 11 of 14
product-of-digits.py
1# Take number
2num = int(input("Enter a number: "))
3
4# Calculate product
5product = 1
6temp = abs(num)
7if temp == 0:
8 product = 0
9else:
10 while temp > 0:
11 digit = temp % 10
12 product *= digit
13 temp //= 10
14
15print(f"Product of digits: {product}")

Output

Enter a number: 234
Product of digits: 24

What's going on

Multiply all digits together.

Key Concepts:

Extract digits using modulo
Multiply with product
Handle zero case