编写十进制转换成二进制的Python代码
时间: 2023-05-20 11:06:33 浏览: 95
十进制转换二进制附带示例
以下是将十进制转换为二进制的Python代码:
def decimal_to_binary(decimal_num):
binary_num = ""
while decimal_num > 0:
remainder = decimal_num % 2
binary_num = str(remainder) + binary_num
decimal_num = decimal_num // 2
return binary_num
decimal_num = 10
binary_num = decimal_to_binary(decimal_num)
print("The binary representation of", decimal_num, "is", binary_num)
阅读全文