Here’s a Python program to print the product of even and odd numbers separately from 1 to 10:
# Function to calculate the product of even and odd numbers from 1 to 10
def product_of_even_and_odd():
even_product = 1
odd_product = 1
for i in range(1, 11):
if i % 2 == 0:
even_product *= i
else:
odd_product *= i
return even_product, odd_product
# Call the function and print the results
even_product, odd_product = product_of_even_and_odd()
print("The product of even numbers from 1 to 10 is:", even_product)
print("The product of odd numbers from 1 to 10 is:", odd_product)-
Initialization:
even_productis initialized to 1 to store the product of even numbers.odd_productis initialized to 1 to store the product of odd numbers.
-
Loop: The
forloop iterates through the numbers from 1 to 10.- If the number is even (
i % 2 == 0), it multiplieseven_productby the number. - If the number is odd, it multiplies
odd_productby the number.
- If the number is even (
-
Output: The products of even and odd numbers are returned and printed.
We can calculate the product of even and odd numbers from 1 to 10 using mathematical properties. Here's how We can approach it:
The even numbers from 1 to 10 are: 2, 4, 6, 8, 10. The product of the first ( n ) even numbers can be written as:
[ \text{Product of evens} = 2 \times 4 \times 6 \times 8 \times 10 = 2^5 \times (1 \times 2 \times 3 \times 4 \times 5) ]
This simplifies to:
[ \text{Product of evens} = 2^5 \times 5! = 32 \times 120 = 3840 ]
The odd numbers from 1 to 10 are: 1, 3, 5, 7, 9. The product of the first ( n ) odd numbers doesn't have a simple formula, so we calculate it directly:
[ \text{Product of odds} = 1 \times 3 \times 5 \times 7 \times 9 = 945 ]
import math
# Function to calculate product of even and odd numbers using formulas
def product_of_even_and_odd():
# Even product: 2^5 * 5!
even_product = (2 ** 5) * math.factorial(5)
# Odd product: 1 * 3 * 5 * 7 * 9 (calculated directly)
odd_product = 1 * 3 * 5 * 7 * 9
return even_product, odd_product
# Call the function and print the results
even_product, odd_product = product_of_even_and_odd()
print("The product of even numbers from 1 to 10 is:", even_product)
print("The product of odd numbers from 1 to 10 is:", odd_product)- Product of even numbers: ( 3840 )
- Product of odd numbers: ( 945 )
- Even Product: We use the mathematical expression ( 2^5 \times 5! ) to calculate the product of even numbers.
- Odd Product: The odd numbers are multiplied directly because there is no simpler formula for their product.
- Factorial: The
math.factorial(5)computes ( 5! = 120 ), used for even product calculation.