Binary to decimal
The first INPUT () is how many bits follow. Then each INPUT () is one bit (0 or 1), from the leftmost bit to the rightmost. DISPLAY the decimal value.
Example: the bits 1, 0, 1, 1 are 1011 in binary, which is 11.
Hint: for each bit, value ← value × 2 + bit.
| Input (one value per INPUT ()) | Expected output |
|---|---|
| 4 1 0 1 1 | 11 |
| 8 1 1 1 1 1 1 1 1 | 255 |
Check runs all 3 tests. Press Run to try the program yourself: a box asks for each INPUT ().
Output
Variables
Robot grid The triangle is the robot, the star is the goal, dark squares are walls.
Check
| Test | Input | Expected | Your output |
|---|
Show a solution
count ← INPUT () value ← 0 REPEAT count TIMES { value ← value * 2 + INPUT () } DISPLAY (value)
Teachers: sign in as a teacher to add this exercise to a class, graded automatically.
Frequently asked questions
How do I solve this exercise?
Complete the starter code in the editor, press Run to try it (type values when INPUT () asks), then press Check to run every test. Step shows the program line by line with its variables.
Where is the solution?
Under the editor, in Show a solution. Try the exercise first: comparing your code with the solution afterwards is a good way to learn another approach.
AP is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site.