Palindrome program in python using function : The palindrome in python program language is using the while loop function and the recursion function. As the consideration of any number in python language if the string remains is same when we reverse it is called the palindrome. The number is said as the palindrome number if the reverse is the same as the original number. The task in this program is to check the number is palindrome or not.
Solution ( Palindrome program in python using function )
num=input (‘Enter any number: ’)
try:
val=int (num)
if num==str (num) [::-1]:
print(‘The given number is PALINDROME’)
else:
print(‘The given number is NOT a PALINDROME’)
except ValueError:
print(“That’s not a valid number, Try Again!”)
Output:-
Enter any number: 595
The given number is PALINDROME
Enter any number: 549
The given number is NOT a PALINDROME
The combination of 3 functions can perform the task easy and the function will extract the number of digits powered by 10 to get number for the iteration and the process is used to test the palindrome of number.
Example:-
import math
defrev (num):
return int(num!=0)and((num%10)*\(10**int(msth.log(num,10)))+\rev(num//10))
test_number=9669669
print(“The original number is:”+str (test_number))
res=test_number==rev (test_number)
print(“Is the number palindrome?:”+str (res))
Output:-
The original number is: 9669669
Is the number palindrome?: True
The string slicing is done by converting the number into string and then reverses it using the string slicing method then comparing it and return value.
Example:-
Test_number=9669669
Print(“The original number is:”+str (test_number))
Res=str (test_number) ==str (test_number) [::-1]
Print(“Is the number palindrome?:”+str (res))
Output:-
The original number is: 9669669
Is the number palindrome?: True
131 the number is considered as the palindrome number because of the number
Remains the same after reversing it.
We have the application to check the number whether it is palindrome or not.
The process of doing the reverse of the number is easy and checks it for the readable, reducing code to operate.
The palindrome is the number or the string that does not affect (unchanged) if it is reversed.
Example:-12321
Output:-
The answer is the palindrome number.
Example:-RACECAR
Output:-
It is also the palindrome string.
The reverse string and grew with the string and called as the palindrome.
The easy way to find the palindrome of the program used by the while loops and checks the number or the string.
Example:-
n=int (input (“Enter a number”))
temp=n
rev=0
while(num>0):
dig=num%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print(“The number is palindrome!”)
else:
print(“Not a palindrome!”)
Output:-
Enter a number: 121
The number is palindrome!
The loop starts to length/2 and checks the first character to last of string and second to the second last one and so on.
Example:-
sef is palindrome (str):
for i in xrange (0, len (str)/2):
if str[i]! =str[len(str)-i-1]:
return false
return true
s=”malayalam”
ans=ispalindrome(s)
if(ans):
print(“yes”)
else:
print(“No”)
Output:-
Yes
The predefined function is “”.join (reversed (string)) is used here to reverse the string.
Example:-
def is palindrome(s):
rev=’’.join (reversed(s))
if(s==rev):
return True
return false
s=”malyalam”
ans=ispalindrome(s)
if(ans):
print(“yes”)
else:
print(“No”)
Output:-
Yes
The method used to take the character of string one by one and then store it in an empty variable.
Then after storing characters the user will compare both strings and check is it palindrome or not.
Example:-
x=”Malayalam”
w=”’
for i in x:
w=i+w
if(x==w):
print(“YES”)
Output:-
Yes