# Palindrome Program in Python

Write a Palindrome Program in Python using While Loop, Functions, and Recursion. Any number could be Palindrome in python if it remained the same when we reversed it. For example, 131 is a palindrome number because this number remains the same after reversing it.

The common approach to check for the Python Palindrome Number is to reverse a given number. Compare the original value with reverse value. If they exactly matched, then it is a Python Palindrome number. Otherwise, it is not a Palindrome number.

## Python Palindrome Program using While Loop

This Palindrome program in Python allows the user to enter any positive integer. Next, this Python palindrome program use While Loop to check whether a given number is Palindrome Number or Not.

```# Python Palindrome Program using While loop

number = int(input("Please Enter any Number: "))

reverse = 0
temp = number

while(temp > 0):
Reminder = temp % 10
reverse = (reverse * 10) + Reminder
temp = temp //10

print("Reverse of a Given number is = %d" %reverse)

if(number == reverse):
print("%d is a Palindrome Number" %number)
else:
print("%d is not a Palindrome Number" %number)```

User Entered value in this palindrome program in Python are Number = 191 and Reverse = 0
Temp = Number
Temp = 191

Python While Loop First Iteration
Reminder = Temp %10
Reminder = 191 % 10 = 1

Reverse = Reverse *10 + Reminder = 0 * 10 + 1
Reverse = 0 + 1 = 1

Temp = Temp //10 = 191 /10
Temp = 19

Python Second Iteration: From the first Python while loop Iteration, the values of both Temp and Reverse changed as Temp = 19 and Reverse = 1

Reminder = 19 % 10 = 9

Reverse = 1 * 10 + 9 = 10 + 9
Reverse = 19

Temp = 19 /10
Temp = 1

Third Iteration: From the second Iteration of palindrome program in Python, the values of Temp = 1 and Reverse = 19

Reminder = 1 % 10 = 1

Reverse = 19 * 10 + 1 = 190 + 1
Reverse = 191

Temp = 1/10
Temp = 0

Here Number = 0. So, the while loop condition fails.

if ( Number == Reverse ) – condition checks whether the user enter number is exactly equal to the Reverse number or not. If this condition is True, then it is Palindrome. Else the given number is not Palindrome number in Python.

## Palindrome Program in Python using Functions

In this Python palindrome program, we defined a function. Within that function, we used the If statement.

```# Python Palindrome Program using Functions

def integer_reverse(number):
reverse = 0

while(number > 0):
Reminder = number % 10
reverse = (reverse * 10) + Reminder
number = number // 10
return reverse

number = int(input("Please Enter any Number: "))

rev = integer_reverse(number)
print("Reverse of a Given number is = %d" %rev)

if(number == rev):
print("%d is a Palindrome Number" %number)
else:
print("%d is not a Palindrome Number" %number)```

Python palindrome number using functions output

``````Please Enter any Number: 1441
Reverse of a Given number is = 1441
1441 is a Palindrome Number
>>>
Reverse of a Given number is = 2021
1202 is not a Palindrome Number``````

## Palindrome Program using Recursion

In this Python Palindrome program, we are checking whether a given number is Palindrome Number or Not using the Recursive Functions concept.

```# Python Palindrome Program using Functions

reverse = 0
def integer_reverse(number):
global reverse

if(number > 0):
Reminder = number % 10
reverse = (reverse * 10) + Reminder
integer_reverse(number // 10)
return reverse

number = int(input("Please Enter any Number: "))

rev = integer_reverse(number)
print("Reverse of a Given number is = %d" %rev)

if(number == rev):
print("%d is a Palindrome Number" %number)
else:
print("%d is not a Palindrome Number" %number)```

Python palindrome number using recursion output

``````Please Enter any Number: 4114
Reverse of a Given number is = 4114
4114 is a Palindrome Number
>>>
=================== RESTART: /Users/suresh/Desktop/simple.py ===================
Reverse of a Given number is = 958
859 is not a Palindrome Number``````

In this palindrome program, within the function declaration,

integer_reverse (Number // 10) – it helps to call the function Recursively with the updated value. If you miss this statement, then after completing the first line, it terminates. For example, Number = 191 then the output is 1

Let’s see the If condition.

if (Number > 0) check whether the number is greater than 0 or not. For Recursive functions, it is very important to place a condition before using the function recursively. Otherwise, we end up in infinite execution (Same like infinite Loop).

## Python program to print Palindrome Numbers from 1 to N

```maximum = int(input(" Please Enter the Maximum Value : "))

print("Palindrome Numbers between 1 and %d are : " %maximum)
for num in range(1, maximum + 1):
temp = num
reverse = 0

while(temp > 0):
Reminder = temp % 10
reverse = (reverse * 10) + Reminder
temp = temp //10

if(num == reverse):
print("%d " %num, end = '  ')```

Python Palindrome Numbers between 1 and 4554 are

`````` Please Enter the Maximum Value : 4554
Palindrome Numbers between 1 and 4554 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   303   313   323   333   343   353   363   373   383   393   404   414   424   434   444   454   464   474   484   494   505   515   525   535   545   555   565   575   585   595   606   616   626   636   646   656   666   676   686   696   707   717   727   737   747   757   767   777   787   797   808   818   828   838   848   858   868   878   888   898   909   919   929   939   949   959   969   979   989   999   1001   1111   1221   1331   1441   1551   1661   1771   1881   1991   2002   2112   2222   2332   2442   2552   2662   2772   2882   2992   3003   3113   3223   3333   3443   3553   3663   3773   3883   3993   4004   4114   4224   4334   4444   4554 ``````