- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to check if binary representation is palindrome?
Here we use different python inbuilt function. First we use bin() for converting number into it’s binary for, then reverse the binary form of string and compare with originals, if match then palindrome otherwise not.
Example
Input: 5 Output: palindrome
Explanation
Binary representation of 5 is 101
Reverse it and result is 101, then compare and its match with originals.
So its palindrome
Algorithm
Palindromenumber(n) /* n is the number */ Step 1: input n Step 2: convert n into binary form. Step 3: skip the first two characters of a string. Step 4: them reverse the binary string and compare with originals. Step 5: if its match with originals then print Palindrome, otherwise not a palindrome.
Example code
# To check if binary representation of a number is pallindrome or not defpalindromenumber(n): # convert number into binary bn_number = bin(n) # skip first two characters of string # Because bin function appends '0b' as # prefix in binary #representation of a number bn_number = bn_number[2:] # now reverse binary string and compare it with original if(bn_number == bn_number[-1::-1]): print(n," IS A PALINDROME NUMBER") else: print(n, "IS NOT A PALINDROME NUMBER") # Driver program if __name__ == "__main__": n=int(input("Enter Number ::>")) palindromenumber(n)
Output
Enter Number ::>10 10 IS NOT A PALINDROME NUMBER Enter Number ::>9 9 IS A PALINDROME NUMBER
- Related Articles
- Java program to check if binary representation is palindrome
- C# program to check if binary representation is palindrome
- Check if binary representation of a number is palindrome in Python
- Golang Program to check if the binary representation of a number is palindrome or not
- Python program to check if binary representation of two numbers are anagram.
- Python program to check if a string is palindrome or not
- Python program to check if a given string is number Palindrome
- Python program to check if the given string is vowel Palindrome
- Python Program to Check String is Palindrome using Stack
- Bash program to check if the Number is a Palindrome?
- C Program to check if an Array is Palindrome or not
- C# program to check if a string is palindrome or not
- C Program to Check if a Given String is a Palindrome?
- Swift Program to check if an Array is Palindrome or not
- Program to check a string is palindrome or not in Python

Advertisements