Python Convert Binary Number to Decimal

By | July 21, 2019

Python Convert Binary Number to Decimal – This Python tutorial gives the basic logic to convert a binary number into its corresponding decimal number. The image number 1 shows the whole binary to decimal number conversion process .

Python convert binary number to decimal program

Python convert binary number to decimal program

The Python source code to convert a binary number into decimal

# This Python script / program is used to input
# a binary number and convert into decimal number
# using a while loop
# author: www.EasyCodeBook.com (c)

num = int(input('Enter a binary number to Convert into Decimal:'))
n=num
decimal=0
i=0

while n!=0:
    rem = n%10
    decimal = decimal + rem * (2**i)
    n = n // 10
    i+=1

print('Decimal Number=',decimal)
            
The output of the Python program is as follows:
Enter a binary number to Convert into Decimal:101
Decimal Number= 5

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *