Palindromic number: Python Code #1

This blog post discusses how to check if an input number is a palindromic number. A palindrome is a number that remains the same when its digits are reversed. For instance, 15651 is a palindrome, but 223 is not a palindrome.

Except for 0, a palindromic number’s leftmost digit must be non-zero.

n = int(input("Enter an input number to check if it is a palindrome: "))
temp_value = n
reverse = 0
while(n > 0):
    digit = n % 10 #using 'Modulus' 10 to find the last digit 
    reverse = reverse * 10 + digit #stores this digit in 'reverse'
    n = n // 10 #removes this digit from the number by using 'integer division'
print("The reverse of your input number is: ", reverse)
if(temp_value == reverse): #tests if the original number and the reverse of the number are equal
    print("Yay :) The input number is a palindrome!")
else: 
    print("Unfortunately, your input number: ", temp_value, " is not a palindrome!")

Feel free to try the code below, by inputting a few numbers:

How did you find this article?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses User Verification plugin to reduce spam. See how your comment data is processed.
error: Sorry, content is protected! For special access, please email contact@bytesofintelligence.co.uk. Thank you.