Python Program for Binary To Decimal Conversion

Find Binary to Decimal Conversion

In this Python Program, we will find the Binary to Decimal Conversion. A Decimal number can be calculated by multiplying every digits of binary number with 2 to the power of the integers starts from 0 to n-1 where n refers as the total number of digits present in a binary number and finally add all of them.

In number system, Binary number uses only two digits of 0, 1 which has base 2. Whereas Decimal Number uses only 10 Symbols: 0, 1, 2, 3, 4, 5, 6, 7 , 8 and 9 which has base 10.

For example:

100101 => 37

Python Program for Binary To Decimal Conversion

Working:

Step 1. Start

Step 2. Take a user input and store into int type num variable.

Step 3. Store num variable value to binary_val.

Step 4. Store 0 to decimal_val.

Step 5. Store 1 to base variable.

Step 6. Run a while loop until num > 0.

Step 7. Store remainder value to rem variable after solving num % 10.

Step 8. Store decimal_val + rem * base value into decimal_val.

Step 9. Store num // 10 into num variable.

Step 10. Store base * 2 value into base variable.

Step 11. Print  decimal_val value.

Step 12. Stop

Python Program:

num = int(input("Enter number:"))
binary_val = num
decimal_val = 0
base = 1
while num > 0:
    rem = num % 10
    decimal_val = decimal_val + rem * base
    num = num // 10
    base = base * 2

print("Binary Number is {} and Decimal Number is {}".format(binary_val, decimal_val))

# This code is contributed by Shubhanshu Arya (Prepinsta Placement Cell Student) 

Output:

Enter number:10
Binary Number is 10 and Decimal Number is 2

 

One comment on “Python Program for Binary To Decimal Conversion”