Write a Python program to convert decimal to binary
In this post, you will learn how to convert a decimal into an equivalent binary number using the Python programming language. Here, we have mentioned three ways to converts a decimal to a binary number.
A decimal number is a base-10 number system. It ranges from 0 to 9 i.e., 0,1,2,3,4,5,6,7,8,9. Any combination of digits is a decimal number such as 61, 22, 912, 0, 5 etc. The main advantages of the decimal number system are easy-to-read, used by humans and easy to manipulate.
A binary number consists of two numbers 0s and 1s. It is expressed in the base-2 numeral system or binary numeral system. As the computer only understands binary language that is 0 or 1, all inputs given to a computer are decoded by it into series of 0's or 1's to process it further. Any combination of 0 and 1 is a binary number such as 1011, 100, 101011, 111011 etc.
Python convert decimal to binary using recursive function
A recursive function is a function that calls itself during its execution. For this, we have defined a function convertDecimalToBinary() that accepts a decimal number as an input and converts it to an equivalent binary number.
# Function to convert a
# decimal number to binary
def convertDecimalToBinary(x):
if x > 1:
convertDecimalToBinary(x // 2)
print(x % 2, end='')
# Get user input
num = int(input("Enter a decimal number: "))
convertDecimalToBinary(num)
Output of the above code-
Enter a decimal number: 23
10111
Enter a decimal number: 53
110101
Enter a decimal number: 90
1011010
Convert decimal to binary using bin() function
The bin() is an in-built function of Python that takes a decimal value and returns an equivalent binary representation of that. This function returns a binary value with the prefix 0b, so depending on the use-case, formatting should be done to remove 0b. The following example demonstrates this-
print("Enter Decimal Number: ", end="")
dec_num = int(input())
bin_num = bin(dec_num).replace("0b", "")
print("\nEquivalent Binary Number:", bin_num)
Output of the above code:
Enter a decimal number: 52
Equivalent Binary Number: 110100
Enter a decimal number: 84
Equivalent Binary Number: 1010100
Enter a decimal number: 93
Equivalent Binary Number: 1011101
Convert decimal to binary without using in-built function
The following program converts a decimal to a binary number without using any in-built functions.
# Function to convert a
# decimal number to binary
def convertDecimalToBinary(num):
return "{0:b}".format(int(num))
# print equivalent binary number
if __name__ == '__main__':
print("Equivalent Binary Number: ",convertDecimalToBinary(43))
print("Equivalent Binary Number: ",convertDecimalToBinary(14))
print("Equivalent Binary Number: ",convertDecimalToBinary(70))
Output of the above code -
Equivalent Binary Number: 101011
Equivalent Binary Number: 1110
Equivalent Binary Number: 1000110
Convert decimal to binary using while loop
In the given Python program, first we import the math module and take decimal number input from the user. Next, iterate through the while loop and for loop to convert the decimal to a binary number.
# Python program to convert decimal to binary
# using while loop
import math
num=int(input("Enter a decimal number: "))
rem=""
while num>=1:
rem+=str(num%2)
num=math.floor(num/2)
binary=""
for i in range(len(rem)-1,-1,-1):
binary = binary + rem[i]
print("Binary format for given number is {0}".format(binary))
Output of the above code:
Enter a decimal number: 66
Binary format for given number: 1000010
Enter a decimal number: 90
Binary format for given number: 1011010
Enter a decimal number: 84
Binary format for given number: 1010100
Related Articles
Python program to map two lists into a dictionaryPython program to input week number and print week day
Python program to count the occurrences of a word in a text file
Multiply all elements in list Python
Python program to multiply two numbers
Convert list to dictionary Python
Convert array to list Python
numpy dot product
glob in Python
Python heap implementation
zip function in Python
Remove last element from list Python
Check if list is empty Python
Remove element from list Python
Python split multiple delimiters
Python loop through list
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword