Write a python function proddigits() that inputs a positive integer number and returns the product of digits of that number. For example, if the input is 422, the function should return 16 (which is equal to 4×2×2 ).
时间: 2024-01-27 11:02:57 浏览: 59
python实现将字符串中的数字提取出来然后求和
Sure, here is a Python function `proddigits()` that takes a positive integer as input and returns the product of its digits:
```python
def proddigits(n):
# Initialize the result
result = 1
# Loop through each digit of the number
while n > 0:
# Get the last digit
digit = n % 10
# Multiply it to the result
result *= digit
# Remove the last digit from the number
n //= 10
# Return the final result
return result
```
You can call this function with a positive integer argument to get the product of its digits. For example:
```python
# Test the function
num = 422
print(proddigits(num)) # Output: 16
```
I hope this helps! Let me know if you have any other questions.
阅读全文