Given a string or an integer,Write a python program to check if a string or an integer is a palindrome.
Palindrome
A given string or a number is said to be palindrome if the opposite or the reverse of the string or a number is same as the orginal string or number.
Example:
1. 12321 - Palindrome
2. 345 - Not Palindrome
3. SOS - Palindrome
4. RUN - Not Palindrome
Python Program
#Function if the given input is a string
def palinStr(n):
#check if the reverse of a string is equal to the original string
print('Palindrome') if n == n[::-1] else print('Not a palindrome')
#Function if the given input is a number
def palinInt(n):
rem = 0
temp = n
while n>0:
#Taking the last number
dig = n%10
#reverse the number from last digit to first digit
rem = rem*10+dig
n = n//10
#check if the reverse of a string is equal to the original string
print("Palindrome") if temp == rem else print("Not a palindrome")
#Taking user input
n = input()
#calling the function base on the user input, A string or a number
palinInt(int(n)) if n.isnumeric() else palinStr(n)
Input:
12321
Output:
Palindrome
Input:
RUN
Output:
Not a palindrome
Comments
Post a Comment