I wrote the program however I wonder how can I edit and show in the output the two products from which I got the output:

def check_palindrome(s):
     """Checks whether the given string is palindrome"""
     return s == s[::-1]
max_product = 0
for i in range(999, 900, -1):
    for j in range(i, 900, -1):
        product = i * j
        if check_palindrome(str(product)):
            max_product = max(max_product, product)
print(max_product)

1 Answers

1
Mr Geek On

In the same way you are updating max_product, you can use two more variables (a and b) and keep updating them when necessary (when product is greater than max_product):

def check_palindrome(s):
     """Checks whether the given string is palindrome"""
     return s == s[::-1]

max_product = a = b = 0

for i in range(999, 900, -1):
    for j in range(i, 900, -1):
        product = i * j
        if check_palindrome(str(product)):
            if product > max_product:     # if greater product
                max_product = product     # update max_product
                a = i                     # update a
                b = j                     # update b

print('%d * %d = %d' % (a, b, max_product))

Also, you can use this for updating, and for shorter code:

max_product, a, b = product, i, j