Menu
CoddyTech

Decimal to Binary

You get a non-negative integer n. Return its binary representation as a string of 0s and 1s, with no leading zeros. The only number whose answer starts with 0 is zero itself, which is written "0".

Function

toBinary(n: integer) → string
ninteger
the number to convert
Returnsstring
the binary digits of n as a string

Constraints

  • 0 ≤ n ≤ 231-1
  • Build the string yourself instead of calling a built-in base conversion.

Examples

Input
n = 13
Output
"1101"
Explanation
13 = 8 + 4 + 1. The places for 8, 4, 2 and 1 hold 1, 1, 0 and 1, which reads 1101.

lock icon+16 hidden tests on Submit

challenge icon

Follow-up

Can you convert n to any base from 2 to 16 with the same loop, using the letters a to f for the digits above 9?

Reset code
def toBinary(n):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

n = 13

Expected

"1101"