Python | 不使用库函数将十进制数转换为二进制
生活随笔
收集整理的這篇文章主要介紹了
Python | 不使用库函数将十进制数转换为二进制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Given a decimal number and we have to convert it into binary without using library function.
給定一個十進制數,我們必須不使用庫函數就將其轉換為二進制數。
Example:
例:
Input: 10Output: 1010Python 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 value def 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 code if __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: 10 Binary of the decimal 10 is: 1010Second run: Input a decimal number: 963 Binary of the decimal 963 is: 1111000011翻譯自: https://www.includehelp.com/python/convert-the-decimal-number-to-binary-without-using-library-function.aspx
總結
以上是生活随笔為你收集整理的Python | 不使用库函数将十进制数转换为二进制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: arcgis出界址点成果表_界址点成果表
- 下一篇: Java DataOutputStrea