Python: Find the product of the units digits in the numbers
Product of Units in List
Write a Python program to find the product of the units digits in the numbers in a given list.
Input: [12, 23] Output: 6 Input: [12, 23, 43] Output: 18 Input: [113, 234] Output: 12 Input: [1002, 2005] Output: 10
Visual Presentation:
Sample Solution-1:
Python Code:
# Define a function named 'test' that takes a list of numbers as input and calculates the product of their units digits
def test(nums):
# Use list comprehension to extract the units digits of each number and join them as a string
# Evaluate the resulting expression to get the product of the units digits
return eval('*'.join([str(x % 10) for x in nums]))
# Create a list of numbers named 'nums'
nums = [12, 23]
# Print a message indicating the task and the original list of numbers
print("Original list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))
# Create another list of numbers named 'nums'
nums = [12, 23, 43]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))
# Create yet another list of numbers named 'nums'
nums = [113, 234]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))
# Create one more list of numbers named 'nums'
nums = [1002, 2005]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))
Sample Output:
Original list of numbers: [12, 23] Product of the units digits in the numbers of the said: 6 Original list of numbers: [12, 23, 43] Product of the units digits in the numbers of the said: 18 Original list of numbers: [113, 234] Product of the units digits in the numbers of the said: 12 Original list of numbers: [1002, 2005] Product of the units digits in the numbers of the said: 10
Flowchart:
Sample Solution-2:
Python Code:
# Define a function named 'test' that takes a list of numbers as input and calculates the product of the absolute values of their units digits
def test(nums):
# Initialize a variable 'prod' to 1 to store the product of units digits
prod = 1
# Iterate through each number in the list 'nums'
for n in nums:
# Update 'prod' by multiplying it with the absolute value of the units digit of the current number
prod *= abs(n % 10)
# Return the final product of the units digits
return prod
# Create a list of numbers named 'nums'
nums = [12, 23]
# Print a message indicating the task and the original list of numbers
print("Original list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))
# Create another list of numbers named 'nums'
nums = [12, 23, 43]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))
# Create yet another list of numbers named 'nums'
nums = [113, 234]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))
# Create one more list of numbers named 'nums'
nums = [1002, 2005]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))
Sample Output:
Original list of numbers: [12, 23] Product of the units digits in the numbers of the said: 6 Original list of numbers: [12, 23, 43] Product of the units digits in the numbers of the said: 18 Original list of numbers: [113, 234] Product of the units digits in the numbers of the said: 12 Original list of numbers: [1002, 2005] Product of the units digits in the numbers of the said: 10
Flowchart:
Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Reverse the case of all strings. For those strings, which contain no letters, reverse the strings.
Next: Remove duplicates from a list of integers, preserving order.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics