Python programs to check if a number is a palindrome or not have been shown here. For example, 1221 is a palindrome because we get the same number irrespective of the direction (left to right or right to left) of reading. But 1234 is not a palindrome as we get 4321 if we read it from right to left.

The algorithm, time complexity and pseudocode of the program have been shown below.

## 1. Algorithm to check if a number is a palindrome or not

1. Take a number n as input.

2. Copy value of n to another variable say m i.e. m = n.

3. Initialize a variable to 0 say reverse = 0.

4. Perform r = n % 10.

5. Perform reverse = reverse * 10 + r.

6. Perform n = n / 10

7. If n = 0 go to step 8 else go to step 3.

8. Check If reverse == m

9. If step 8 is true then n is palindrome else n is not palindrome.

## 2. Pseudocode to check if a number is a palindrome or not

Input : A number $n$

Output : Palindrome or Not Palindrome

1. Procedure checkPalindrome($n$):

2. $reverse \leftarrow 0$

3. $m \leftarrow n$

4. Repeat until $n \neq 0$

5. $reverse \leftarrow reverse * 10 + (n \mod 10)$

6. $n \leftarrow n / 10$

7. If $reverse == m$:

8. Return "Palindrome"

9. Else:

10. Return "Not Palindrome"

11. End Procedure

## 3. Time complexity to check if a number is a palindrome or not

Time Complexity: O(log(n))

Where n is the input number.

## 4. Python Program & output to check if a number is a palindrome or not using iteration

Code has been copied
# ***************************************
#        alphabetacoder.com
# Python program to check if a number is
# a palindrome or not using iteration
# ***************************************

# take input of the number
n = int(input("Enter the number = "))

# initialize
m = n
revnum = 0

# find the reverse
while n != 0:
# extract the unit digit
r = n % 10
# store the reverse number
# give appropriate positional value
# of each digit
revnum = revnum * 10 + r
# divide the number by 10
n = n // 10

# check for palindrome
if m == revnum:
print(m, "is a palindrome!")
else:
print(m, "is not a palindrome!")


Output

Enter the number = 50505

50505 is a palindrome!

## 5. Python Program & output to check if a number is a palindrome or not using recursion

Code has been copied
# ***************************************
#        alphabetacoder.com
# Python program to check if a number is
# a palindrome or not using recursion
# ***************************************

# recursive function to check if
# a number is a palindrome or not
def check_palindrome(n, num, rev):
if n == 0:
# if reverse is same as original
if num == rev:
return 1
else:
return 0
else:
return check_palindrome(n // 10, num, (rev * 10 + n % 10))

def main():
# take input of the number
num = int(input("Enter the number = "))

# check for palindrome
# by calling the function
# If the function returns 1,
# then the number is palindrome
# else the number is not palindrome
if check_palindrome(num, num, 0) == 1:
print(num, "is a palindrome!")
else:
print(num, "is not a palindrome!")

# driver code
main()


Output

Enter the number = 61916

61916 is a palindrome!