```
# Simple functions to convert from decimal to binary and vice-versa
def toBinary( decimalNumber ):
quotient = 1
remainder = 0
tmpNum = decimalNumber
finalNumberList = []
n = ""
#e.g. take 14...
while quotient != 0:
remainder = decimalNumber % 2 #14 % 2 = 0
quotient = decimalNumber / 2 #14 / 2 = 7
decimalNumber = quotient # 7 % 2 = 1 and so on...
finalNumberList.insert( 0, remainder )
# Used because all numbers are in a list, i.e. convert to string
for num in finalNumberList:
n += str( num )
return n
def toDecimal( binaryNumber ):
multiplier = 0
number = 0
for el in binaryNumber[ : : -1 ]:
number += int( el ) * ( 2**multiplier )
multiplier += 1
return number
print toDecimal( "1110" )
print toBinary( 45 )
```

**Are you able to help answer this sponsored question?**

Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.

Recommended Topics