{"id":26764,"date":"2022-04-18T01:27:31","date_gmt":"2022-04-17T19:57:31","guid":{"rendered":"https:\/\/python-programs.com\/?p=26764"},"modified":"2022-04-18T01:27:31","modified_gmt":"2022-04-17T19:57:31","slug":"python-program-to-find-nearest-palindrome-of-a-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-nearest-palindrome-of-a-number\/","title":{"rendered":"Python Program to Find Nearest Palindrome of a Number"},"content":{"rendered":"
Palindrome:<\/strong><\/p>\n If a number reads the same both forward and backward, it is called a Palindrome number. And the insane part is that it doesn\u2019t just apply to numbers. Even if a string reads the same in both directions, it is always a Palindrome!<\/p>\n For Example –\u00a0 <\/strong>121, 131, etc<\/p>\n when we reverse 121 we get the same 121. Hence it is a Palindrome number.<\/p>\n 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.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n NOTE:<\/strong> 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.<\/p>\n","protected":false},"excerpt":{"rendered":" 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\u2019t just apply to numbers. Even if a string reads the same in both directions, it is always a Palindrome! For Example –\u00a0 121, 131, etc when we reverse 121 we …<\/p>\nGiven Number = 620<\/pre>\n
The Nearest palindrome of the given number{ 620 } is:\r\n626<\/pre>\n
Given Number = 120<\/pre>\n
The Nearest palindrome of the given number{ 120 } is:\r\n121<\/pre>\n
Program to Find Nearest Palindrome of a Number in Python<\/h2>\n
Method #1: Using Slicing (Static Input)<\/h3>\n
\n
# Create a function say Nearest_palindrome() which accepts the given number as an argument\r\ndef Nearest_palindrome(gvn_numb):\r\n # Loop recursively using the while loop\r\n while(1):\r\n # Check if the Reverse of the given number is equal to the given number using slicing \r\n # and if conditional statement.\r\n if gvn_numb ==int(str(gvn_numb)[::-1]):\r\n # If it is true, then return that corresponding number.\r\n return gvn_numb\r\n # Increment the value of given number by 1 i.e, add 1 to the given number until\r\n # it becomes a palindrome\r\n gvn_numb+=1\r\n\r\n# Give the number as static input and store it in a variable.\r\ngvn_numb = 120\r\n# Pass the given number to the above created Nearest_palindrome() function and\r\n# print the result.\r\nprint(\"The Nearest palindrome of the given number{\", gvn_numb, \"} is:\")\r\nprint(Nearest_palindrome(gvn_numb))<\/pre>\n
The Nearest palindrome of the given number{ 120 } is:\r\n121<\/pre>\n
Method #2: Using Slicing (User Input)<\/h3>\n
\n
# Create a function say Nearest_palindrome() which accepts the given number as an argument\r\ndef Nearest_palindrome(gvn_numb):\r\n # Loop recursively using the while loop\r\n while(1):\r\n # Check if the Reverse of the given number is equal to the given number using slicing \r\n # and if conditional statement.\r\n if gvn_numb ==int(str(gvn_numb)[::-1]):\r\n # If it is true, then return that corresponding number.\r\n return gvn_numb\r\n # Increment the value of given number by 1 i.e, add 1 to the given number until\r\n # it becomes a palindrome\r\n gvn_numb+=1\r\n\r\n# Give the number as as user input using the int(input()) function and store it in a variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n# Pass the given number to the above created Nearest_palindrome() function and\r\n# print the result.\r\nprint(\"The Nearest palindrome of the given number{\", gvn_numb, \"} is:\")\r\nprint(Nearest_palindrome(gvn_numb))<\/pre>\n
Enter some random number = 620\r\nThe Nearest palindrome of the given number{ 620 } is:\r\n626<\/pre>\n