codescracker
python

Python Program to Convert Decimal to Binary



« Previous Program Next Program »

In this article, we've created some programs in Python to convert decimal number to its equivalent binary value. The decimal number must be entered by user at run-time. Here are the list of programs:

Note - Before creating these programs, if you're not aware about steps used for the conversion, then refer to Decimal to Binary Formula and Steps to get every required things about the topic.

Decimal to Binary using List

To convert decimal to binary number in Python, you have to ask from user to enter a number in decimal number system, then convert that number into its binary equivalent as shown in the program given below:

print("Enter the Decimal Number: ")
dnum = int(input())
i = 0
bnum = []
while dnum!=0:
    rem = dnum%2
    bnum.insert(i, rem)
    i = i+1
    dnum = int(dnum/2)

i = i-1
print("\nEquivalent Binary Value is:")
while i>=0:
    print(end=str(bnum[i]))
    i = i-1
print()

Here is its sample run:

decimal to binary python

Now enter any number in decimal number system say 50 and then press ENTER key to convert and print its equivalent value in binary number system as shown in the snapshot given below:

convert decimal to binary python

The dry run of above program with user input 50 goes like:

Modified Version of Previous Program

This is the modified version of previous program. The end= is used to skip printing of an automatic newline using print(). The str() method is used to convert any type of value to a string type.

print("Enter Decimal Number: ", end="")
d = int(input())
i = 0
b = []
while d!=0:
    b.insert(i, d % 2)
    i = i+1
    d = int(d / 2)

i = i-1
print(end="\nEquivalent Binary Value = ")
while i>=0:
    print(end=str(b[i]))
    i = i-1
print()

Here is its sample run with user input, 105 as decimal number:

python convert decimal to binary

Decimal to Binary without List

This program does the same job as of previous program, but without using list. You can take its dry run with yourself to understand it in better way:

print("Enter Decimal Number: ", end="")
dnum = int(input())

bnum = 0
mul = 1
while dnum>0:
    rem = dnum%2
    bnum = bnum+(rem*mul)
    mul = mul*10
    dnum = int(dnum/2)

print("\nEquivalent Binary Value =", bnum)

Here is its sample run with user input, 345 as decimal number input:

python convert decimal to binary

Decimal to Binary using Function

This program uses a user-defined function named DecToBin() that receives a number as its argument and returns its equivalent binary value. Therefore, we've passed the decimal number entered by user to this function, so that its return value gets initialized to bnum. And the value of this variable gets printed on output as binary equivalent of given decimal number by user at run-time:

def DecToBin(d):
    b = 0
    m = 1
    while d>0:
        b = b + ((d%2)*m)
        m = m*10
        d = int(d/2)
    return b

print("Enter Decimal Number: ", end="")
dnum = int(input())

bnum = DecToBin(dnum)
print("\nEquivalent Binary Value =", bnum)

This program produces same output as of previous program.

Decimal to Binary using Class

This is the last program, created using a class named CodesCracker. Class is an object-oriented feature of Python. To access anything like member function of the class, we've to create an object before doing this

Therefore an object named ob is created of class CodesCracker. So that, we can access its member function named DecToBin() using dot (.) operator to do the job. Rest of the things works similar to a normal function.

class CodesCracker:
    def DecToBin(self, d):
        b = 0
        m = 1
        while d>0:
            b = b + ((d%2)*m)
            m = m*10
            d = int(d/2)
        return b

print("Enter Decimal Number: ", end="")
dnum = int(input())

ob = CodesCracker()
bnum = ob.DecToBin(dnum)
print("\nEquivalent Binary Value =", bnum)

Decimal to Binary using bin()

This is the program that uses bin(), a predefined function of Python to do the same job as previous programs have done. The bin() function returns binary equivalent of value passed as its argument.

print("Enter Decimal Number: ", end="")
dnum = int(input())

bnum = bin(dnum)
print("\nEquivalent Binary Value =", bnum)

Here is its sample run with user input, 49:

python decimal to binary using function

Note - To remove first two characters, add [2:] just after bnum while printing its value to print all its element starting from second index. Or just replace the following statement:

print("\nEquivalent Binary Value =", bnum)

with the statement given below:

print("\nEquivalent Binary Value =", bnum[2:])

now the output with same user input, that is 49 looks like:

python program decimal to binary

Shortest Python Code for Decimal to Binary Conversion

This is the shortest Python code for decimal to binary conversion. This program doesn't print any message. The program receives input from user, and print its binary equivalent, without any extra things like previous programs have:

dnum = int(input())
print(bin(dnum)[2:])

Here is its sample run with user input 59:

python shortest code for decimal to binary

Same Program in Other Languages

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker