Python Convert the decimal number to binary without using a library function

Decimal to paired in Python: Here, we will figure out how to change over the given decimal number to the double number without utilizing any library work in Python?

Given a decimal number and we need to change over it into paired without utilizing library work.

Example:

Python code to change over decimal to binary:

# 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 recursion
    bin_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: 1010

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

Leave a Comment

error: Alert: Content is protected!!