Write a program in Python that takes the output above in BIN

Write a program in Python that takes the output above in BINARY and converts it to DECIMAL. Please help

Solution

Hi,

I am little confused with your question requirement so putting logic for both way of conversion, Binary to Decimal and Decimal to Binary.

print\" DECIMAL TO BINARY AND BINARY TO DECIMAL CONVERSION\"

print\"=====================================================\"
print\" Enter 1 if you want to convert dec to bin\"
print\" Enter 2 if you want to convert bin to dec\"
print\"=====================================================\"
x=input(\"Enter your choice: \")
if x==1:
i=1
s=0
dec=input(\"Enter decimal to be converted: \")
while dec>0:
rem=dec%2
s=s+(i*rem)
dec=dec/2
i=i*10
print \"The binary of the given number is \",s,\'.\'
else:
bin=raw_input (\'Enter binary to be converted: \')
n=len(bin)
res=0
for i in range(1,n+1):
res=res+ int(bin[i-1])*2**(n-i)
print \"The decimal of the given binary is \",res,\'.\'
print\"=====================================================\"
raw_input()

Write a program in Python that takes the output above in BINARY and converts it to DECIMAL. Please helpSolutionHi, I am little confused with your question requi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site