I remember first having to make these sorts of functions whilst doing GCSE Computing many years ago :-)
Decimal Number to Binary:
def decimal_to_binary(decimal_num):
binary_num = ""
if decimal_num == 0:
return "0"
while decimal_num > 0:
remainder = decimal_num % 2
binary_num = str(remainder) + binary_num
decimal_num //= 2
return binary_num
decimal_number = 100
binary_number = decimal_to_binary(decimal_number)
print(binary_number)
Binary Number to Decimal:
def binary_to_decimal(binary_num):
decimal_num = 0
power = len(binary_num) - 1
for digit in binary_num:
decimal_num += int(digit) * (2 ** power)
power -= 1
return decimal_num
binary_number = "101011"
print(binary_to_decimal(binary_number))