Skip to content

Latest commit

 

History

History
89 lines (66 loc) · 2.97 KB

File metadata and controls

89 lines (66 loc) · 2.97 KB

Program to print the product of even and odd numbers separately from 1 to 10

Here’s a Python program to print the product of even and odd numbers separately from 1 to 10:

Code:

# 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)

Explanation:

  1. Initialization:

    • even_product is initialized to 1 to store the product of even numbers.
    • odd_product is initialized to 1 to store the product of odd numbers.
  2. Loop: The for loop iterates through the numbers from 1 to 10.

    • If the number is even (i % 2 == 0), it multiplies even_product by the number.
    • If the number is odd, it multiplies odd_product by the number.
  3. 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:

For Even Numbers:

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 ]

For Odd Numbers:

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 ]

Python Code:

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)

Output:

  • Product of even numbers: ( 3840 )
  • Product of odd numbers: ( 945 )

Explanation:

  1. Even Product: We use the mathematical expression ( 2^5 \times 5! ) to calculate the product of even numbers.
  2. Odd Product: The odd numbers are multiplied directly because there is no simpler formula for their product.
  3. Factorial: The math.factorial(5) computes ( 5! = 120 ), used for even product calculation.