Loading...
Loading...
Loading...
A computer lab worksheet gives numbers in binary, written with the digits 0 and 1, like 1011. Read the number and print its decimal value. In binary, each place is worth twice the place to its right: 1011 = 1×8 + 0×4 + 1×2 + 1×1 = 11.
If any digit is not 0 or 1, print Not binary.
nextLong()).Not binary.Example 1
Input
1011
Output
11
8 + 2 + 1 = 11.
Example 2
Input
1021
Output
Not binary
The digit 2 cannot appear in binary.
Try the problem first. Open one hint at a time.
Take digits from the right with % 10. The rightmost digit is worth 1, the next 2, then 4, and so on.
Keep a place value that doubles every round, and add digit * place to the answer.
If a digit is 2 or more, remember it in a boolean and break.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor