Convert Decimal to Binary Python Programs


In this article, we will look at 5 different ways to convert decimal to binary in Python.

Decimal and Binary Numbers

Decimal numbers are the numbers we use in our day-to-day life. It is a base 10 number system. The decimal number system has 10 digits from 0 to 9.

Binary numbers are the numbers that are represented in base 2. The binary number system has 2 digits 0 and 1.

Any decimal number can be converted to a binary number. For example, 10 in decimal is 1010 in binary.

Decimal numbers can be represented in binary format.

(0)10 = (0)2
(1)10 = (1)2
(2)10 = (10)2
(5)10 = (111)2
(10)10 = (1010)2
(15)10 = (1111)2
(50)10 = (110010)2
(100)10 = (1100100)2

To convert a decimal number to binary, we need to divide the decimal number by 2 and write the remainder in reverse order.

decimal to binary conversion
Decimal to Binary Conversion

1. Decimal to Binary Python using bin() function

The bin() function is built-in function in Python. It can be used to convert a decimal number to a binary number.

It takes a single argument as input and returns the binary equivalent of the input number. The input number can be of any base but the output is always in base 2.

n = 10
print(bin(n))
# Output: 0b1010

n = 20
print(bin(n))
# Output: 0b10100

2. Decimal to Binary Python by Division by 2 (using array)

In the last method, we used a built-in python function to convert decimal to binary. In this method, we will use a simple algorithm to convert decimal numbers to binary.

Algorithm

  1. Take a variable bin (to store the binary number) and initialize it to 0.
  2. Take an empty array (list in python) arr to store the binary digits.
  3. Now execute a while loop until the number is greater than 0.
  4. Inside the loop, append the remainder of the number divided by 2 to the array and divide the number by 2.
  5. Now reverse the array because the binary digits we get are in reverse order.
  6. Now iterate through the array and multiply the variable bin by 10 and add the array element to it.
  7. Finally, return the variable bin.
def dectobin(n):
    bin = 0
    arr = []
    while n > 0:
        arr.append(n % 2)
        n //= 2
    arr.reverse()
    for i in arr:
        bin = bin * 10 + i
    return bin
  
n = int(input("Enter a decimal number: "))
print(dectobin(n))

Output:

Enter a decimal number: 25
Binary number: 11001
decimal to binary example
Decimal to Binary Example

3. Decimal to Binary Python by Division by 2 (without using array)

In this method, we will use the same algorithm as in the previous method but we will not use an array to store the binary digits.

With a little modification, we can store the binary digits in a variable and return it.

Algorithm

  1. Take a variable bin (to store the binary number) and initialize it to 0.
  2. Take a variable i and initialize it to 1. This variable will be used to multiply the binary digits with the power of 10.
  3. Now run a while loop until the number is greater than 0.
  4. Inside the loop, add the remainder of the number divided by 2 to the variable bin and multiply it by the variable i.
  5. Now divide the number by 2 and multiply the variable i by 10.
  6. Finally, return the variable bin.
def dectobin(n):
    bin = 0
    i = 1
    while n > 0:
        bin = bin + (n % 2) * i
        n //= 2
        i *= 10
    return bin
  
n = int(input("Enter a decimal number: "))
print(dectobin(n))
decimal to binary example
Decimal to Binary Example

Output:

Enter a decimal number: 50
Binary number: 110010

4. Decimal to Binary Python using Recursion

Recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem.

We will call the same function recursively with the quotient of the number divided by 2 as the argument.

def dectobin(n):
    if n == 0:
        return 0
    else:
        return n % 2 + 10 * dectobin(n // 2)
  
n = int(input("Enter a decimal number: "))
print(dectobin(n))

Output:

Enter a decimal number: 100
Binary number: 1100100