Python Practice › Loop Programs › Program 23

Product of digits

EasyLoop Programs · 23 of 90

Write a Python program to calculate the product of the digits of a number.

Advertisement
Input
4352
Output
Product of digits: 120

Approach

  1. Same as the sum, but start at 1 and multiply.

Solution

Python
n = 4352product = 1while n > 0:    product *= n % 10    n //= 10print("Product of digits:", product)
Output
Product of digits: 120

Line by line

Line 2

The product starts at 1, because starting at 0 would make everything 0.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Leave a Comment