Hackbright Code Challenges

Decimal To Binary

Decimal To Binary

Whiteboard

Harder

Challenge

Medium

Concepts

General

Download

dec2bin.zip

Solution

Decimal To Binary: Solution


Binary numbers are base 2 numbers – there are only 0s and 1s in the numbers. So, for example:

Decimal

Binary

0

0

1

1

2

10

3

11

4

100

5

101

6

110

7

111

8

1000

9

1001

10

1010

11

1011

12

1100

13

1101

14

1110

15

1111

16

10000

Write a function that is given a decimal number and returns the binary number as a string.

For example:

>>> dec2bin(6)
'110'

We’ve written dec2bin.py for you, but the main function, dec2bin, isn’t implemented:

def dec2bin(num):
    """Convert a decimal number to binary representation."""

Complete the conversion function in dec2bin.py.