Product of Digits

Print the product of digits of a given number.

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

Output

Enter a number: 234
Product of digits: 24

What's going on

Extract digits and multiply them.

Key Concepts:

Extract last digit using modulo
Multiply with product
Remove last digit using integer division
Handle negative numbers with abs()