write a program to check whether a number is palindrome or not in python

A number is said to be a palindrome if the reverse of a number matches the number itself.  For example, let's take  121 as a number. The reverse of 121 is also 121.

Let's take a non palindrome number 12345  if we reverse the number it will become 54321.  if we try to compare 12345 with 54321 they  both don't match.  so the number is a non palindrome number.

 

write a program to check whether a number is palindrome or not in python

 

 

Write a program to check whether a number is palindrome or not in python

Let's take an input number from the user  and store it in temp.  Use a while loop to reverse the number using modulus addition and division by 10.  modulus will give the last digit of a number and the last digit of the river by multiplying by 10.  divide the number by 10 to trim the last digit off temp.

Once the while loop completes the reverse  variable will be storing the reverse of a number.  use if statement to compare number as well as reverse number and print the appropriate result if they are matching

number = int(input("Enter a Number : "))
temp = number
reverse = 0
while temp != 0:
   last_digit = temp % 10
   reverse = reverse * 10 + last_digit
   temp = temp // 10

if reverse == number:
   print("Number is Palindrome")
else:
   print("Number is not a Palindrome")


OUTPUT 1:
Enter a Number : 121
Number is Palindrome


OUTPUT 2:
Enter a Number : 12345
Number is not a Palindrome


OUTPUT 3:
Enter a Number : 12121
Number is Palindrome

 

Conclusion:  try to run the program by yourself by changing the input number and checking if the number is palindrome or not.



Post a Comment

0 Comments