Write a python function proddigits() that inputs a positive integer number and returns the product of digits of that number.
时间: 2024-01-03 15:04:52 浏览: 29
Sure, here's the code for the proddigits() function in Python:
```python
def proddigits(n):
"""Returns the product of digits of a positive integer number n."""
product = 1
while n > 0:
digit = n % 10
product *= digit
n //= 10
return product
```
You can call this function by passing a positive integer number as an argument, like this:
```python
result = proddigits(12345)
print(result) # Output: 120
```
This will calculate the product of digits of the number 12345, which is 1 * 2 * 3 * 4 * 5 = 120.
相关问题
用for loop回答下面问题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 ).
Sure, here is the modified `proddigits()` function that uses a `for` loop to calculate the product of digits:
```python
def proddigits(n):
# Initialize the result
result = 1
# Convert the number to a string for iteration
n_str = str(n)
# Loop through each digit of the number
for digit in n_str:
# Convert the digit back to an integer
digit_int = int(digit)
# Multiply it to the result
result *= digit_int
# 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.
• Write a python code that find all the prime number in a int list. • Test your code with different inputs.
Here's a Python code that finds all the prime numbers in a given integer list:
```python
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
def find_primes(lst):
return [num for num in lst if is_prime(num)]
# Test the function
lst1 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
lst2 = [17, 19, 20, 23, 28, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
lst3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
print(find_primes(lst1)) # Output: [2, 3, 5, 7, 11, 13]
print(find_primes(lst2)) # Output: [17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
print(find_primes(lst3)) # Output: [2, 3, 5, 7, 11, 13]
```
The `is_prime()` function checks whether a given number is prime or not. It returns `True` if the number is prime, and `False` otherwise. The `find_primes()` function takes a list of integers as input and returns a list of prime numbers in that list.
In the test cases, we have used three different lists with different integer values. The output shows the prime numbers present in these lists.