Program to check if binary representation is palindrome using Python

by | Jul 15, 2021 | Python Programs

Home » Python » Python Programs » Program to check if binary representation is palindrome using Python

Introduction

The task is to check whether the binary representation of the given number is palindrome or not.

Program to check if binary representation is palindrome using Python

Program

def check(ip_num):
# Convert the given number into binary format
     convert = bin(ip_num)
# Ignore the first two characters (0b) appended while covertion
     convert = convert[2:]
# Reverse the output and check
     return convert == convert[-1::-1]


ip_num = int(input("Enter the number: "))
print("The  binary representation of {0} is Palindrome ? True/False : {1} ".format(ip_num,check(ip_num)) )

Output

Program to check if binary representation is palindrome using Python Output

Explanation

Approach:

  • Convert the given number to binary representation using python in-built bin() function.
  • Reverse the converted string. (Ignore the first two character as while conversion 0b is inserted at start of string)
  • Compare the original converted string with reversed string. If they are same then the binary string is palindrome.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author