Area 07 · Number system
Decimal to binary, octal and hex
Show a decimal number in binary, octal and hexadecimal with your own repeated-division routine, and count its 1 bits.
- Spec no.
- HO-13
- Level
- Warm-up
- Time box
- 45 min
- Reference
- Java · C#
- Grading
- Self-assessed · rubric is ours, not DICT's
01 Coverage
| Area | Sub-skills practised | Role |
|---|---|---|
| 06 Control structures | Iteration | Also |
| 07 Number system | Binary, Octal, Decimal, Hexadecimal | Primary |
Sign up to start coding practice
Read the task freely. An account is required before editing and running a solution. Code runs also need an internet connection.
Sign up or sign in02 Requirements
- Read decimal integers, one per line, until q.
- Accept 0 to 65535. Print Out of range: x or Not a number: x otherwise.
- Convert with one toBase(n, base) function using repeated division. Do not use built-in radix conversions.
- Count 1 bits with & 1 and a shift.
- Print n: bin b, oct o, hex H, ones k with upper-case hex digits.
03 Input and output
- Decimal integers, one per line
- q to quit
- One line per input
10
255
0
70000
abc
4095
q10: bin 1010, oct 12, hex A, ones 2
255: bin 11111111, oct 377, hex FF, ones 8
0: bin 0, oct 0, hex 0, ones 0
Out of range: 70000
Not a number: abc
4095: bin 111111111111, oct 7777, hex FFF, ones 12Match the sample exactly first, then write at least two test inputs of your own.
04 Edge cases to test
- 0 (prints 0 in every base)
- 65535 and 65536
- A negative number
- Text
05 Starter outline and hints
DIGITS = "0123456789ABCDEF"toBase(n, b): while n > 0 → append DIGITS[n % b]; n /= b; reverseones(n): while n > 0 → count += n & 1; n >>= 1
Hint 1
Remainders come out lowest digit first, so reverse at the end (or insert at the front).
06 Practice rubric
| Criterion | Sub-skills | Look for | Pts |
|---|---|---|---|
| Conversion | BinaryOctalHexadecimal | Correct for all three bases, including 0. | 40 |
| Own algorithm | Decimal | Repeated division; digits from a lookup string. | 25 |
| Bit counting | Binary | Uses & and shift, not a string count. | 15 |
| Loop and validation | Iteration | Range and number checks; q ends the loop. | 20 |
| Total | 100 | ||
Our own weighting for practice. It is not the DICT scoring scheme.
07 Self-check
Tick only what you have verified by running your own program.
Loading saved checks…
08 Reference solution
Both versions compiled and reproduced the sample output on Java 21 and .NET 8 when this site was built. Reference solutions are in Java and C#. The 2023 FAQ copy also lists C, C++ and Visual Basic 6.0 / .NET; the same structure carries over, but you will need your language's own file and database APIs.
Try the whole brief first. The reference is one correct approach, not the only one, and reading it early removes most of the practice value.
09 Exam-day habits
- Work without internet search to simulate exam conditions.
- Type the sample input yourself and compare your output line by line before you look at the reference.