DAY 28 of PYTHON top 100 questions : from Basic to Advanced !!

Hello, I'm Priya Chakraborty, a dedicated B.Tech student in Electrical Engineering at Siliguri Institute Of Technology. I'm an enthusiastic learner, constantly seeking to expand my skill set and knowledge base. With a solid foundation in programming languages such as C, R, Python, and MySQL, I'm poised to tackle complex technical challenges.
But my passions extend beyond the realm of engineering. I'm also an aspiring content writer, driven by a curiosity to communicate ideas, both technical and non-technical, in a way that captivates and educates.
My journey is defined by a relentless pursuit of self-improvement, coupled with strong communication skills. I believe in the power of continuous learning and the importance of sharing knowledge.
Write a Python program to find the largest palindrome made from the product of two n-digit numbers.
#Write a Python program to find the largest palindrome made from the product of two n-digit numbers.
def is_palindrome(number):
return str(number) == str(number)[::-1]
def largest_palindrome_product(n):
if n <= 0:
return None
lower_bound = 10**(n - 1)
upper_bound = 10**n - 1
largest_palindrome = 0
for i in range(upper_bound, lower_bound - 1, -1):
for j in range(i, lower_bound - 1, -1):
product = i * j
if product < largest_palindrome:
break
if is_palindrome(product):
largest_palindrome = product
return largest_palindrome
n = int(input("Enter the number of digits (n): "))
result = largest_palindrome_product(n)
if result:
print(f"The largest palindrome made from the product of two {n}-digit numbers is: {result}")
else:
print("Please enter a positive integer for n.")
Output :
Enter the number of digits (n): 8
The largest palindrome made from the product of two 8-digit numbers is: 9999000000009999
def is_palindrome(number):: This line defines a function namedis_palindromethat takes a number as input.return str(number) == str(number)[::-1]: In this function, it converts the input number to a string, and then it checks if the string is equal to its reverse (achieved by slicing with[::-1]). If the string representation of the number is the same forwards and backward, the function returnsTrue, indicating that the number is a palindrome. Otherwise, it returnsFalse.def largest_palindrome_product(n):: This line defines another function namedlargest_palindrome_product, which takes an integernas input.if n <= 0:: This line checks if the inputnis less than or equal to zero. Ifnis not a positive integer, the function returnsNone, indicating that the input is not valid.lower_bound = 10**(n - 1): This line calculates the lower bound for n-digit numbers. For example, ifnis 2, this setslower_boundto 10 (the smallest two-digit number).upper_bound = 10**n - 1: This line calculates the upper bound for n-digit numbers. For example, ifnis 2, this setsupper_boundto 99 (the largest two-digit number).largest_palindrome = 0: This line initializes a variablelargest_palindrometo 0, which will be used to store the largest palindrome found.for i in range(upper_bound, lower_bound - 1, -1):: This line starts a loop that iterates through the range of values fromupper_bounddown tolower_bound - 1, in descending order.for j in range(i, lower_bound - 1, -1):: Inside the outer loop, this line starts an inner loop that iterates through the same range of values as the outer loop, but starting from the current value ofidown tolower_bound - 1, in descending order. This is used to generate products of two n-digit numbers.product = i * j: This line calculates the product of the current values ofiandj.if product < largest_palindrome:: This line checks if theproductis less than thelargest_palindromefound so far. If it is, it means we've already found a palindrome greater than this product, so there's no need to continue checking products for thisi. This is an optimization to reduce unnecessary computations.break: If the product is smaller than thelargest_palindrome, it breaks out of the inner loop and continues with the next value ofi.if is_palindrome(product):: This line checks if theproductis a palindrome using theis_palindromefunction defined earlier.largest_palindrome = product: If the product is a palindrome and it's larger than thelargest_palindromefound so far, it updates thelargest_palindromewith this new value.return largest_palindrome: After both loops have been completed, the function returns the largest palindrome found.n = int(input("Enter the number of digits (n): "): This line takes user input to specify the number of digits (n).result = largest_palindrome_product(n): This line calls thelargest_palindrome_productfunction with the user-provided value ofnand stores the result in theresultvariable.if result:: This line checks if a valid result was obtained (i.e., ifresultis notNone).print(f"The largest palindrome made from the product of two {n}-digit numbers is: {result}"): If a valid result is found, it prints the result in a formatted string.else:: If the inputnwas not a positive integer (i.e.,resultisNone), this line is executed.print("Please enter a positive integer for n."): It prints a message indicating that the input was not valid.
If you are a beginner and want to know more about Python programming, you can read my Basics of Python blogs (From part 1 to part 15).
HAPPY LEARNING !!!



