Decimal to binary

Write a number in binary without using bin().

What to build

A loop that builds the digits of a number in base 2.

Requirements

  • Read n.
  • Print n in binary, with no leading zeros.
  • Do not use bin().

Input

One whole number n.

Output

One line of 0s and 1s.

Constraints

n is from 0 to 1000000000.

Example

Input:

10

Output:

1010

Why it works

Dividing by 2 repeatedly gives the binary digits from last to first. Adding each new digit at the front puts them in the right order.

Examples

Example 1 · Input
10
Expected output
1010
Example 2 · Input
1
Expected output
1

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

n % 2 gives the last binary digit, and n // 2 removes it.

Hint 2

The digits come out last first, so put each new digit at the front of the text.

Hint 3

0 needs special care, because the loop never runs for it.

Solution

Show one correct solution (try it yourself first)
n = int(input())
if n == 0:
    print("0")
else:
    digits = ""
    while n > 0:
        digits = str(n % 2) + digits
        n = n // 2
    print(digits)

Lesson: Repeating with while →