# Python Program to Print Palindrome Numbers in a Range

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â€™t just apply to numbers. Even if a string reads the same in both directions, it is always a Palindrome!

Given lower limit and upper limit range and the task is to print the palindrome numbers in a given range.

Examples:

Example1:

Input:

Given lower limit = 50
Given upper limit= 130

Output:

The palindrome numbers between 50 and 130 are:
55 66 77 88 99 101 111 121

Example2:

Input:

Given lower limit = 1
Given upper limit= 300

Output:

The palindrome numbers between 1 and 300 are:
1 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

## Program to Print Palindrome Numbers in a Range in Python

Below are the ways to print the palindrome numbers in a given range:

### Method #1: Using While Loop (Static Input)

Approach:

• Give the lower limit range as static input and store it in a variable.
• Give the upper limit range as static input and store it in another variable.
• Loop from lower limit range to upper limit range using For loop.
• Inside the loop, give the iterator value as the number of the for loop.
• Take another variable say duplicate_num and initialize it with the given number
• Set the variable reverse_number to 0.
• Loop while number > 0.
• Multiply reverse_number by 10 and add the remainder to reverse_number like below
• reverse_number = (reverse_number * 10) + remainder
• Divide the given number by 10 to remove the last digit.
• If the duplicate_num and reverse_number are equal then it is a palindrome.
• Print “duplicate_num “to get the above palindrome number.
• The Exit of the Program.

Below is the implementation:

# Give the lower limit range as static input and store it in a variable.
gvn_lowrlmt = 50
# Give the upper limit range as static input and store it in another variable.
gvn_upprlmt = 130
print("The palindrome numbers between",
gvn_lowrlmt, "and", gvn_upprlmt, "are:")
# Loop from lower limit range to upper limit range using For loop.
for m in range(gvn_lowrlmt, gvn_upprlmt+1):
# Inside the loop, give the iterator value as the number of the for loop.
given_num = m
# taking another variable to store the copy of original number
# and initialize it with given num
duplicate_num = given_num
# Take a variable reverse_number and initialize it to null
reverse_number = 0
# using while loop to reverse the given number
while (given_num > 0):
# implementing the algorithm
# getting the last digit
remainder = given_num % 10
reverse_number = (reverse_number * 10) + remainder
given_num = given_num // 10
# if duplicate_num and reverse_number are equal then it is palindrome
if(duplicate_num == reverse_number):
# Print "duplicate_num "to get the above palindrome number.
print(duplicate_num, end=" ")

Output:

The palindrome numbers between 50 and 130 are:
55 66 77 88 99 101 111 121

### Method #2: Using While loop (User Input)

Approach:

• Give the lower limit range as user input using the int(input()) function and store it in a variable.
• Give the upper limit range as user input using the int(input()) function and store it in another variable.
• Loop from lower limit range to upper limit range using For loop.
• Inside the loop, give the iterator value as the number of the for loop.
• Take another variable say duplicate_num and initialize it with the given number
• Set the variable reverse_number to 0.
• Loop while number > 0.
• Multiply reverse_number by 10 and add the remainder to reverse_number like below
• reverse_number = (reverse_number * 10) + remainder
• Divide the given number by 10 to remove the last digit.
• If the duplicate_num and reverse_number are equal then it is a palindrome.
• Print “duplicate_num “to get the above palindrome number.
• The Exit of the Program.

Below is the implementation:

# Give the lower limit range as user input using the int(input()) function
# and store it in a variable.
gvn_lowrlmt = int(input("Enter some random Number = "))
# Give the upper limit range as user input using the int(input()) function
# and store it in another variable.
gvn_upprlmt = int(input("Enter some random Number = "))
print("The palindrome numbers between",
gvn_lowrlmt, "and", gvn_upprlmt, "are:")
# Loop from lower limit range to upper limit range using For loop.
for m in range(gvn_lowrlmt, gvn_upprlmt+1):
# Inside the loop, give the iterator value as the number of the for loop.
given_num = m
# taking another variable to store the copy of original number
# and initialize it with given num
duplicate_num = given_num
# Take a variable reverse_number and initialize it to null
reverse_number = 0
# using while loop to reverse the given number
while (given_num > 0):
# implementing the algorithm
# getting the last digit
remainder = given_num % 10
reverse_number = (reverse_number * 10) + remainder
given_num = given_num // 10
# if duplicate_num and reverse_number are equal then it is palindrome
if(duplicate_num == reverse_number):
# Print "duplicate_num "to get the above palindrome number.
print(duplicate_num, end=" ")

Output:

Enter some random Number = 1
Enter some random Number = 300
The palindrome numbers between 1 and 300 are:
1 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

Dive into numerous Python Programming Language Examples for practice and get the best out of the tutorial and learn python one step at a time.