Python Program to Find Nearest Palindrome of a Number

Palindrome:

If a number reads the same both forward and backward, it is called a Palindrome number. And the insane part is that it doesn’t just apply to numbers. Even if a string reads the same in both directions, it is always a Palindrome!

For Example –  121, 131, etc

when we reverse 121 we get the same 121. Hence it is a Palindrome number.

In this article, let us look at how to use Python to find the nearest palindrome to a given number if the provided number isn’t a palindrome.

Examples:

Example1:

Input:

Given Number = 620

Output:

The Nearest palindrome of the given number{ 620 } is:
626

Example2:

Input:

Given Number = 120

Output:

The Nearest palindrome of the given number{ 120 } is:
121

Program to Find Nearest Palindrome of a Number in Python

Using the slicing operator in Python, we can determine whether a given number or string is a palindrome. We will also use the slicing operator to get the nearest palindrome of an integer. A given number’s nearest palindrome is found by adding 1 to the number until it is a palindrome. This is accomplished by recursively using a while loop. When the condition is satisfied, the number is output.

Method #1: Using Slicing (Static Input)

Approach:

  • Create a function say Nearest_palindrome() which accepts the given number as an argument
  • Loop recursively using the while loop
  • Check if the Reverse of the given number is equal to the given number using slicing and if conditional statement.
  • If it is true, then return that corresponding number.
  • Increment the value of a given number by 1 i.e, add 1 to the given number until it becomes a palindrome
  • Give the number as static input and store it in a variable.
  • Pass the given number to the above created Nearest_palindrome() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Create a function say Nearest_palindrome() which accepts the given number as an argument
def Nearest_palindrome(gvn_numb):
    # Loop recursively using the while loop
    while(1):
        # Check if the Reverse of the given number is equal to the given number using slicing 
        # and if conditional statement.
        if gvn_numb ==int(str(gvn_numb)[::-1]):
            # If it is true, then return that corresponding number.
            return gvn_numb
        # Increment the value of given number by 1 i.e, add 1 to the given number until
        # it becomes a palindrome
        gvn_numb+=1

# Give the number as static input and store it in a variable.
gvn_numb = 120
# Pass the given number to the above created Nearest_palindrome() function and
# print the result.
print("The Nearest palindrome of the given number{", gvn_numb, "} is:")
print(Nearest_palindrome(gvn_numb))

Output:

The Nearest palindrome of the given number{ 120 } is:
121

Method #2: Using Slicing (User Input)

Approach:

  • Create a function say Nearest_palindrome() which accepts the given number as an argument
  • Loop recursively using the while loop
  • Check if the Reverse of the given number is equal to the given number using slicing and if conditional statement.
  • If it is true, then return that corresponding number.
  • Increment the value of a given number by 1 i.e, add 1 to the given number until it becomes a palindrome
  • Give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number to the above created Nearest_palindrome() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Create a function say Nearest_palindrome() which accepts the given number as an argument
def Nearest_palindrome(gvn_numb):
    # Loop recursively using the while loop
    while(1):
        # Check if the Reverse of the given number is equal to the given number using slicing 
        # and if conditional statement.
        if gvn_numb ==int(str(gvn_numb)[::-1]):
            # If it is true, then return that corresponding number.
            return gvn_numb
        # Increment the value of given number by 1 i.e, add 1 to the given number until
        # it becomes a palindrome
        gvn_numb+=1

# Give the number as as user input using the int(input()) function and store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Pass the given number to the above created Nearest_palindrome() function and
# print the result.
print("The Nearest palindrome of the given number{", gvn_numb, "} is:")
print(Nearest_palindrome(gvn_numb))

Output:

Enter some random number = 620
The Nearest palindrome of the given number{ 620 } is:
626

NOTE: Because we can’t use the slice operator on an integer, we first typecast it to string. The string is then typecast to an integer since we need to iterate the loop again by adding 1 if it is not a palindrome.