Convert a Number into Binary and Check if It’s a Palindrome

Have you ever wondered whether the binary representation of a number reads the same forwards and backwards? If yes, you are exploring the concept of a binary palindrome. In this post, we’ll understand how to convert a number into binary and check whether that binary representation is a palindrome using a simple Python program.


🔢 What is Binary?

Binary is a number system that uses only 0s and 1s. It is the fundamental language of computers, where every piece of data is ultimately represented in binary form.

Examples:

  • Decimal 5 → Binary 101
  • Decimal 9 → Binary 1001
  • Decimal 12 → Binary 1100

🔁 What is a Palindrome?

A palindrome is a sequence that reads the same forward and backward.

Examples:

  • "121" → Palindrome ✅
  • "101" → Palindrome ✅
  • "110" → Not a palindrome ❌
  • "1001" → Palindrome ✅

So, a binary palindrome means the binary form of a number looks the same in both directions.


💡 Problem Statement

We need to:

  1. Take a number as input
  2. Convert it into binary
  3. Check whether the binary string is a palindrome

🧠 Approach

To solve this problem, we follow these steps:

Step 1: Convert Number to Binary

In Python, we can use the built-in bin() function:

bin(number)

This returns a string like "0b101" — so we remove the "0b" prefix.


Step 2: Check Palindrome

A string is a palindrome if:

string == string[::-1]

💻 Python Program

def is_binary_palindrome(n):
    # Convert number to binary and remove '0b' prefix
    binary = bin(n)[2:]
    
    # Check if binary string is palindrome
    return binary == binary[::-1]

# Example usage
num = int(input("Enter a number: "))

binary_form = bin(num)[2:]
print("Binary Representation:", binary_form)

if is_binary_palindrome(num):
    print("The binary number is a palindrome ✔")
else:
    print("The binary number is NOT a palindrome ❌")

🧪 Sample Output

Input:

Enter a number: 9

Output:

Binary Representation: 1001
The binary number is a palindrome ✔

🚀 Key Takeaways

  • Binary is the base-2 number system used in computing
  • A palindrome reads the same forwards and backwards
  • We can easily check binary palindromes using Python string slicing
  • This is a great beginner problem for learning both number systems and string manipulation

🎯 Conclusion

Understanding binary palindromes helps strengthen your knowledge of number systems and programming logic. With just a few lines of Python code, you can convert numbers and analyze their binary patterns efficiently.

Keep practicing and explore more such logical problems to improve your coding skills!