Write a python function proddigits() that inputs a positive integer number and returns the product of digits of that number.
时间: 2024-01-03 16:04:52 浏览: 155
TCA6416A扩展芯片
Here's a Python function `proddigits()` that takes a positive integer as input and returns the product of its digits:
```python
def proddigits(number):
product = 1
while number > 0:
digit = number % 10
product *= digit
number //= 10
return product
```
This function uses a while loop to iterate through each digit of the input number. It calculates the product of each digit by using the modulus operator (%) to get the last digit and then dividing the number by 10 to shift the digits to the right. The loop continues until the number becomes 0.
Here's an example of how to use this function:
```python
number = 12345
result = proddigits(number)
print("Product of digits of", number, "is", result)
```
This would output:
```
Product of digits of 12345 is 120
```
which is the product of the digits 1 x 2 x 3 x 4 x 5.
阅读全文