{"id":20025,"date":"2021-09-06T13:32:57","date_gmt":"2021-09-06T08:02:57","guid":{"rendered":"https:\/\/python-programs.com\/?p=20025"},"modified":"2021-11-22T18:36:29","modified_gmt":"2021-11-22T13:06:29","slug":"python-program-to-print-palindrome-numbers-in-a-range","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-palindrome-numbers-in-a-range\/","title":{"rendered":"Python Program to Print Palindrome Numbers in a Range"},"content":{"rendered":"
In the previous article, we have discussed Python Program to get First Element of each Tuple in a List<\/a><\/p>\n 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 Given lower limit and upper limit range and the task is to print the palindrome numbers in a given range.<\/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 Below are the ways to print the palindrome numbers in a given range:<\/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 Dive into numerous Python Programming Language Examples<\/a> for practice and get the best out of the tutorial and learn python one step at a time.<\/p>\n In the previous article, we have discussed Python Program to get First Element of each Tuple in a List 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 …<\/p>\nGiven lower limit = 50\r\nGiven upper limit= 130<\/pre>\n
The palindrome numbers between 50 and 130 are:\r\n55 66 77 88 99 101 111 121<\/pre>\n
Given lower limit = 1\r\nGiven upper limit= 300<\/pre>\n
The palindrome numbers between 1 and 300 are:\r\n1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111 121 131 141 151 161 171 181 191 202 212 222 232 242 252 262 272 282 292<\/pre>\n
Program to Print Palindrome Numbers in a Range in Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Give the lower limit range as static input and store it in a variable.\r\ngvn_lowrlmt = 50\r\n# Give the upper limit range as static input and store it in another variable.\r\ngvn_upprlmt = 130\r\nprint(\"The palindrome numbers between\",\r\n gvn_lowrlmt, \"and\", gvn_upprlmt, \"are:\")\r\n# Loop from lower limit range to upper limit range using For loop.\r\nfor m in range(gvn_lowrlmt, gvn_upprlmt+1):\r\n # Inside the loop, give the iterator value as the number of the for loop.\r\n given_num = m\r\n # taking another variable to store the copy of original number\r\n # and initialize it with given num\r\n duplicate_num = given_num\r\n # Take a variable reverse_number and initialize it to null\r\n reverse_number = 0\r\n # using while loop to reverse the given number\r\n while (given_num > 0):\r\n # implementing the algorithm\r\n # getting the last digit\r\n remainder = given_num % 10\r\n reverse_number = (reverse_number * 10) + remainder\r\n given_num = given_num \/\/ 10\r\n # if duplicate_num and reverse_number are equal then it is palindrome\r\n if(duplicate_num == reverse_number):\r\n # Print \"duplicate_num \"to get the above palindrome number.\r\n print(duplicate_num, end=\" \")\r\n<\/pre>\n
The palindrome numbers between 50 and 130 are:\r\n55 66 77 88 99 101 111 121<\/pre>\n
Method #2: Using While loop (User Input)<\/h3>\n
\n
# Give the lower limit range as user input using the int(input()) function\r\n# and store it in a variable.\r\ngvn_lowrlmt = int(input(\"Enter some random Number = \"))\r\n# Give the upper limit range as user input using the int(input()) function \r\n# and store it in another variable.\r\ngvn_upprlmt = int(input(\"Enter some random Number = \"))\r\nprint(\"The palindrome numbers between\",\r\n gvn_lowrlmt, \"and\", gvn_upprlmt, \"are:\")\r\n# Loop from lower limit range to upper limit range using For loop.\r\nfor m in range(gvn_lowrlmt, gvn_upprlmt+1):\r\n # Inside the loop, give the iterator value as the number of the for loop.\r\n given_num = m\r\n # taking another variable to store the copy of original number\r\n # and initialize it with given num\r\n duplicate_num = given_num\r\n # Take a variable reverse_number and initialize it to null\r\n reverse_number = 0\r\n # using while loop to reverse the given number\r\n while (given_num > 0):\r\n # implementing the algorithm\r\n # getting the last digit\r\n remainder = given_num % 10\r\n reverse_number = (reverse_number * 10) + remainder\r\n given_num = given_num \/\/ 10\r\n # if duplicate_num and reverse_number are equal then it is palindrome\r\n if(duplicate_num == reverse_number):\r\n # Print \"duplicate_num \"to get the above palindrome number.\r\n print(duplicate_num, end=\" \")\r\n<\/pre>\n
Enter some random Number = 1\r\nEnter some random Number = 300\r\nThe palindrome numbers between 1 and 300 are:\r\n1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111 121 131 141 151 161 171 181 191 202 212 222 232 242 252 262 272 282 292<\/pre>\n
\n