Answer to Question #211592 in Python for sudheer

Question #211592

Given a range represented by two positive integers L and R. Find the number lying in the range having the maximum product of the digits. (Inclusive of L and R).

For example, if the given T is 2, read the L and R of the first test case in the next line. If the given L and R are 1 and 10 respectively. As 9 is the maximum product of digits, so the output for the first test case is 9.

If the L and R are 15 to 30 respectively. The product of the digits of number 29 is 18. As 18 is the maximum product in the range, so the output should be 18.

input:

2 1

1 10

15 30

output:

9

29


1
Expert's answer
2021-06-30T03:14:31-0400
n = int(input())
numbers = []
for i in range(n):
    numbers.append([int(n) for n in input().split()])
 
productDigits = []
for i in range(n):
    maxValue = 0
    maxNumber = 0
    for number in range(numbers[i][0],numbers[i][1] + 1):
        product = 1 
        tempNumber = number
        while True:
            product *= (tempNumber%10)
            tempNumber = tempNumber // 10
            if  tempNumber == 0:
                if product > maxValue:
                    maxValue = product
                    maxNumber = number
                break
    productDigits.append(maxNumber)
for pd in productDigits:
    print(pd)







Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS