Write a function that checks whether an input number is a palindrome or not. [Note: A number or a string is called palindrome if it appears same when written in reverse order also. For example, 12321 is a palindrome while 123421 is not a palindrome]
Question
Write a function that checks whether an input number is a palindrome or not.
[Note: A number or a string is called palindrome if it appears same when written in reverse order also. For example, 12321 is a palindrome while 123421 is not a palindrome]
✨ Free AI Tools for You
Powered by Chatterbot AI
Answer
Here is a function in Python to check whether an input number is a palindrome or not:
def is_palindrome(number):
original = str(number)
reverse = original[::-1]
if original == reverse:
return True
else:
return False
# Example usage
num = int(input("Enter a number: "))
if is_palindrome(num):
print(f"{num} is a palindrome")
else:
print(f"{num} is not a palindrome")
This function is_palindrome
converts the input number to a string, reverses the string, and then checks if the reversed string is equal to the original string. If they are equal, the function returns True
, indicating that the number is a palindrome; otherwise, it returns False
.
India's 1st AI Doubt Solver for CBSE, JEE, and NEET
Ask a Question for Freeand then it's just ₹212 a month