Python Check Binary Representation of Given Number is Palindrome or Not

To check if binary representation of a number is palindrome in python; Through this tutorial, i am going to show you how to check if binary representation of a number is palindrome in python.

A positive number or string is said to be a palindrome if the reverse of the number or string is equal to the given number or string. For example, 141 is a palindrome but 142 is not.

Python program to check if binary representation is palindrome

  • Python program to convert a given decimal number to binary number
  • Python Program to Check Whether the Binary Equivalent of a Given Number is Palindrome

Python program to convert a given decimal number to binary number

  • Get input number from user in python program.
  • Convert number into a binary number.
  • Print result.
# Python program to convert a given decimal number to binary number
# take input the number from user
x=int(input('Enter a number: '))
# convert into binary number
y=int(bin(x)[2:]) 
# display the result
print("The binary representation of number:", y)

Output

Enter a number:  17
The binary representation of number: 10001

Python Program to Check Whether the Binary Equivalent of a Given Number is Palindrome

  • Get the input number from user by using input() function.
  • Convert number into a binary number.
  • To check the binary representation is a palindrome or not.
  • Print result.
# Python Program to Check Whether the Binary Equivalent of a Given Number is Palindrome
# take input the number user
x=int(input('Enter a number: '))
# converting to binary
y=int(bin(x)[2:])
# reversing the binary 
out=str(y)[::-1] 
print("The binary representation of number:", y)
# checking the palindrome
if int(out)==y:
    print("The binary representation of the number is a palindrome.")
else:
    print("The binary representation of the number is not a palindrome.")

Output

Enter a number:  17
The binary representation of number: 10001
The binary representation of the number is a palindrome.

Recommended Python Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.