Check if product of digits of a number at even and odd places is equal in Python

In this problem, we need to check whether the product of digits at odd positions equals the product of digits at even positions in a number. The positions are counted from right to left, starting with position 1.

For example, in the number 2364:

  • Odd positions (1st, 3rd): 4, 6 ? Product = 4 × 6 = 24
  • Even positions (2nd, 4th): 3, 2 ? Product = 3 × 2 = 6

Let's correct this with a proper example where products are equal ?

Algorithm

To solve this, we will follow these steps ?

  • If number
  • Initialize odd_product = 1, even_product = 1
  • Extract digits alternately, multiplying odd position digits and even position digits
  • Compare the two products and return the result

Implementation

def solve(num):
    if num < 10:
        return False
    
    odd_product = 1
    even_product = 1
    position = 1
    
    while num > 0:
        digit = num % 10
        
        if position % 2 == 1:  # Odd position
            odd_product *= digit
        else:  # Even position
            even_product *= digit
            
        num = num // 10
        position += 1
    
    return odd_product == even_product

# Test with examples
numbers = [2146, 1234, 2364]

for num in numbers:
    result = solve(num)
    print(f"Number: {num}, Equal products: {result}")
Number: 2146, Equal products: True
Number: 1234, Equal products: False
Number: 2364, Equal products: False

Example Breakdown

Let's trace through the number 2146 ?

def solve_with_trace(num):
    print(f"Analyzing number: {num}")
    
    if num < 10:
        return False
    
    odd_product = 1
    even_product = 1
    position = 1
    original_num = num
    
    while num > 0:
        digit = num % 10
        
        if position % 2 == 1:  # Odd position
            odd_product *= digit
            print(f"Position {position} (odd): digit {digit}, odd_product = {odd_product}")
        else:  # Even position
            even_product *= digit
            print(f"Position {position} (even): digit {digit}, even_product = {even_product}")
            
        num = num // 10
        position += 1
    
    print(f"Final: odd_product = {odd_product}, even_product = {even_product}")
    return odd_product == even_product

solve_with_trace(2146)
Analyzing number: 2146
Position 1 (odd): digit 6, odd_product = 6
Position 2 (even): digit 4, even_product = 4
Position 3 (odd): digit 1, odd_product = 6
Position 4 (even): digit 2, even_product = 8
Final: odd_product = 6, even_product = 8

Alternative Approach Using String

We can also solve this by converting the number to a string and accessing positions directly ?

def solve_string_approach(num):
    if num < 10:
        return False
    
    num_str = str(num)
    length = len(num_str)
    
    odd_product = 1
    even_product = 1
    
    for i in range(length):
        digit = int(num_str[length - 1 - i])  # Right to left
        position = i + 1
        
        if position % 2 == 1:
            odd_product *= digit
        else:
            even_product *= digit
    
    return odd_product == even_product

# Test both approaches
test_numbers = [2146, 1234, 1122]

for num in test_numbers:
    result1 = solve(num)
    result2 = solve_string_approach(num)
    print(f"{num}: Method1={result1}, Method2={result2}")
2146: Method1=False, Method2=False
1234: Method1=False, Method2=False
1122: Method1=True, Method2=True

Conclusion

Both approaches work effectively to check if products of digits at odd and even positions are equal. The mathematical approach is more memory-efficient, while the string approach is more intuitive for understanding position-based operations.

Updated on: 2026-03-25T15:22:26+05:30

390 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements