Here, we will figure out how to check the binary portrayal of a given number is a palindrome or not in Python programming language?
A positive number or string is said to be a palindrome if the switch of the number or string is equivalent to
the given number or string. For example, 132231 is a palindrome yet 13243 isn’t.
In this issue, a number will be given by the client and we need to change over it into a binary
number and after this, we will check the binary portrayal is a palindrome or not. Prior to going to do the given errand, we will figure out how to change over a number into a binary number.
Python program to change over a given decimal number (P) to binary number:
# input the number
P=int(input('Enter a number: '))
# convert into binary number
s=int(bin(P)[2:])
# printing the result
print("The binary representation of number:", s)
Output:
RUN 1:
Enter a number: 17
The binary representation of number: 10001
RUN 2:
Enter a number: 100
The binary representation of number: 1100100
As we have figured out how to change over a decimal number into a binary number in the above program and the binary portrayal of 90 isn’t a palindrome and this is our fundamental errand to check palindrome utilizing Python. Presently, we can undoubtedly explain it. Along these lines, how about we start composing the program to check the binary portrayal of the given number is a palindrome or not in Python.
Program:
# input the number
P=int(input('Enter a number: '))
# converting to binary
s=int(bin(P)[2:])
# reversing the binary
r=str(s)[::-1]
# checking the palindrome
if int(r)==s:
print("The binary representation of the number is a palindrome.")
else:
print("The binary representation of the number is not a palindrome.")
Output:
RUN 1:
Enter a number: 27
The binary representation of the number is a palindrome.
RUN 2:
Enter a number: 100
The binary representation of the number is not a palindrome.
In Python, str(P)[::- 1] is utilized to invert a number P which is a property of cutting.