Product of Digits

Easy · rating 950 · Math, Strings

Read a non-negative integer n. Print the product of its digits.

Constraints: 0 ≤ n ≤ 1018

Editorial

Approach

Treat the number as a string of digits and multiply them together, starting from 1. Any number containing a 0 digit produces a product of 0.

p = 1
for ch in input():
    p *= int(ch)
print(p)

Complexity

Time: O(d) in the number of digits. Space: O(1).

Related problems

Open Product of Digits in Code Arena →