Binary text
Inputs
n is a whole number from 0 to 1000.
Your tasks
- Create an empty string
bitsand a copyrestofn. - Use a
whileloop that runs whilerestis more than 0. Each round, put the remainder ofrestdivided by 2 at the front ofbits, then halverest(rounded down). - Complete
toBinaryso it returnsbits, or "0" whennis 0. Return it, do not print it.
Example
With n = 10, toBinary returns "1010".
Rules
- Do not use a built-in base conversion.
Test data
TEST DATA 1: n is 10
TEST DATA 2: n is 0
Practice focus
While loops and early exits, for when you don't know how many rounds you need. Binary search, the Euclidean algorithm and graph search are all while loops with a clear stopping rule.