# Python Program for Binary To Decimal Conversion

## Binary to Decimal Conversion

In this article we will discuss binary to decimal conversion in Python. For this purpose we need to take a binary integer number from user and convert that binary integer number to its decimal equivalent form and then print the converted number on to the screen. 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.

### Working :

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.

### Methods Discussed :

1. Algorithmic way (Binary to Decimal)
2. Inbuilt Method (Binary to Decimal)

### Algorithm :

• While num is greater then zero
• Store the unit place value of num to a variable (rem)
• Completely divide Num by 10 and multiply base with 2

### Time and Space Complexities

• Time Complexity – O(N), where N is the count of the digits in the binary number
• Space Complexity – O(1), Constant Space

### Python Code :

Run
```num = 10
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 {}\nDecimal Number is {}".format(binary_val, decimal_val))```

### Output :

```Binary Number is 10
Decimal Number is 2```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

### 21 comments on “Python Program for Binary To Decimal Conversion”

• Pranudh

num=int(input())
decimal=0
power=0
while num>0:
rem=num%10
decimal+=rem*(2**power)
num=num//10
power+=1
print(decimal)

• Nachiket

bin=’10001′
sum=0
n=len(bin)-1
for i in range(n,-1,-1):
sum=sum+(int(bin[i])*pow(2,i))
print(sum)

• Mathavan

ls=[]
for i in str(n):
ls.append(i)
k=len(ls)-1
g=0
i=0
while(i<len(ls)):
if(ls[i]=="1"):
g+=2**k k-=1
i+=1 print(g)

• D

n=input()
s=0
k=len(n)
d=(2**(k-1))
for i in (n):
s=s+(int(i)*d)
k-=1
print(n,”decimal form is”,s)

• Palak

n = int(input())
le = len(str(n))
dec = 0
l = le-1

for i in str(n):
dec = dec + (int(i)*(pow(2 , l)))
l = l-1

print(dec)

• Saurabh

def binary_to_deci(n):
decimal =0
n1 =str(n)
for i in range(0,len(n1)):
decimal+= (int(n1[i])*pow(2,len(n1)-1-i))
return decimal

n= 1001
print(“Deciaml value of {} is”.format(n),binary_to_deci(n))

• cherith

n=int(input())
l=list(str(n))
print(l)
l.reverse()
print(l)
decimal=0
for i in range(0,len(l)):
decimal+=int(l[i])*(2**i)

print(decimal)

n=int(input())
ls=[]
for i in str(n):
ls.append(i)
k=len(ls)-1
g=0
i=0
while(i<len(ls)):
if(ls[i]=="1"):
g+=2**k

k-=1
i+=1

print(g)