DEV Community

Durga Pokharel
Durga Pokharel

Posted on • Edited on

3 2

Day 2 of 100DaysOfCode: Decimal to Binary

This second day of my #100daysofcode. I gave my time to learn new concept about Python from Coursera in a topic Python Data Structure(File read/write and lists) etc. Again I completed one challenge. Here is a code which was converting 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

Day 2 of #100DaysOfCode
Decimal to Binary conversion.#Python #programming #CodeNewbie pic.twitter.com/NmttA1HSsK

— Durga Pokharel (@mathdurga) December 25, 2020

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (2)

Collapse
 
otumianempire profile image
Michael Otu

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

Yeah nice idea. Thanks

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay