DEV Community

loading...

Day 2 of 100DaysOfCode: Decimal to Binary

iamdurga profile image Durga Pokharel ・1 min read

This a 2nd day of my #100daysofcode. I learned lots of new things about Python from Coursera's Python Data Structures, Specially File read/write and lists etc. I also completed one challenge. Here is a code about of mine solving Decimal to Binary using python.

dec = int(input("Enter a decimal number. "))
curr_dec = dec
rem = 0
octal = []
num = 2
while True:
    if curr_dec % num == 0:
        rem = 0
        curr_dec = int(curr_dec/num)
        octal.append(rem)
    else:
        if curr_dec < num:
            octal.append(curr_dec)
            break
        rem = curr_dec % num
        curr_dec = int(curr_dec/num)
        octal.append(rem)

octal = octal[::-1]
x = [str(i) for i in octal ]
x = "".join(x)
print(x)


Enter fullscreen mode Exit fullscreen mode

Discussion (2)

pic
Editor guide
Collapse
otumianempire profile image
Otu Michael

I hope you save your code on some remote server.. rewrite this code again using functions and until you get there, you are doing good..

Collapse
iamdurga profile image
Durga Pokharel Author

Yeah nice idea. Thanks