Monday, June 14, 2021

Check a number is palindrome in python

 palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar.

Ex: 121, ATA etc...

We can do this in many ways, here are some.

1. With the help of while loop

2. With the help of inbuilt functions

While loop:

number=int(input("Enter a num:"))
tem=number
rever=0
while(number>0):
    digit=number%10
    rever=rever*10+digit
    number=number//10
if(tem==rever):
    print("The number : "+str(number)+" is palindrome.")
else:
    print("The number : "+str(number)+" is not a palindrome")


In-built function:

strin=input(("Enter a str:"))
if(strin==strin[::-1]):
    print("The str is a palindrome")
else:
    print("Not a palindrome")