Write a python program to find the next smallest palindrome of a specified number


The program finds the smallest palindrome greater than the given number.

  • The program first defines a function smallest_Palindrome that takes an integer num as input. It then converts the integer to a string using str(num) and stores it in a variable numstr.
  • The function then enters a for loop that iterates from num+1 to the maximum value representable by an integer in Python, which is given by sys.maxsize.
  • Inside the loop, the program checks if the string representation of the current number, str(i), is equal to the reverse of the string, str(i)[::-1]. If it is, then the function returns the current number i.
  • If no palindrome greater than num is found in the loop, the program continues to search until it reaches the maximum value of an integer in Python.
  • Finally, the program prompts the user to enter a number, num, and calls the smallest_Palindrome function with num as an argument. The smallest palindrome greater than num is then printed as output.

Source Code

import sys
def smallest_Palindrome(num):
    numstr = str(num)
    for i in range(num+1,sys.maxsize):
        if str(i) == str(i)[::-1]:
            return i
 
num = int(input("Enter the Number :"))			
print(smallest_Palindrome(num))

Output

Enter the Number :23454
23532

Example Programs