TechPython Program to find if a number is palindrome or not

Python Program to find if a number is palindrome or not

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. Examples include “A man, a plan, a canal: Panama,” “Able was I ere I saw Elba,” and “Madam, in Eden, I’m Adam.” In the case of numbers, palindrome numbers are the numbers which remains the same when reversed, for example 121, 11, 555 etc.

Python Program to find if a number is palindrome or not using in-built function

def isPalindrome(s): 
    return s == s[::-1] 
  
# Driver code 
s = "madam"
ans = isPalindrome(s) 
  
if (ans): 
    print("Yes") 
else: 
    print("No") 

Python Program to find if a number is palindrome or not without using in-built function

num = 121
temp = num
rev = 0
  
while(num > 0):
    dig = num % 10
    rev = rev * 10 + dig
    num = num // 10
  
# If the original and reverse of number is same 
# then it's a palindrome otherwise not 
if (temp == rev):
    print("The number is a palindrome!")
else:
    print("The number isn't a palindrome!")

Python program to check if a given number is palindrome or not by taking input from the user

num = int(input("Enter a number: "))

temp = num
rev = 0

while temp > 0:
    digit = temp % 10
    rev = rev * 10 + digit
    temp //= 10

if num == rev:
    print(num, "is a palindrome number")
else:
    print(num, "is not a palindrome number")

The program prompts the user to enter a number, assigns the value to the variable ‘num’, creates a copy of the number in the variable ‘temp’, and initializes ‘rev’ as 0. Using a while loop, the program continues until ‘temp’ becomes 0. In each iteration, it gets the last digit of ‘temp’ using the modulus operator and then adds it to the ‘rev’ variable after multiplying it by 10. The program then removes the last digit from ‘temp’ using the floor division operator. Finally, it checks if the original number is equal to the reversed number and prints whether it is a palindrome or not.

IR Digital Media Team
IR Digital Media Teamhttps://www.isrgrajan.com/
IR Digital Media Team is a member of Digital Pradesh, a collective of journalists, reporters, writers, editors, lawyers, advocates, professors, and scholars affiliated with the Digital Pradesh.

Latest Updates