2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > Python | 不使用库函数将十进制数转换为二进制

Python | 不使用库函数将十进制数转换为二进制

时间:2023-04-11 21:29:26

相关推荐

Python | 不使用库函数将十进制数转换为二进制

Given a decimal number and we have to convert it into binary without using library function.

给定一个十进制数,我们必须不使用库函数就将其转换为二进制数。

Example:

例:

Input: 10Output: 1010

Python code to convert decimal to binary

Python代码将十进制转换为二进制

# Python code to convert decimal to binary# function definition# it accepts a decimal value # and prints the binary valuedef decToBin(dec_value):# logic to convert decimal to binary # using recursionbin_value =''if dec_value > 1:decToBin(dec_value//2)print(dec_value % 2,end = '')# main codeif __name__ == '__main__':# taking input as decimal # and, printing its binary decimal = int(input("Input a decimal number: "))print("Binary of the decimal ", decimal, "is: ", end ='')decToBin(decimal)

Output

输出量

First run:Input a decimal number: 10Binary of the decimal 10 is: 1010Second run:Input a decimal number: 963Binary of the decimal 963 is: 1111000011

翻译自: /python/convert-the-decimal-number-to-binary-without-using-library-function.aspx

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。