A Level Sample Question

From TRCCompSci - AQA Computer Science
Revision as of 13:26, 29 November 2016 by 195.195.168.33 (talk) (Figure 7)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Question 6

One method for converting a decimal number into binary is to repeatedly divide by 2 using integer division. After each division is completed, the remainder is output and the integer result of the division is used as the input to the next iteration of the division process. The process repeats until the result of the division is 0.

Outputting the remainders in the sequence that they are calculated produces the binary digits of the equivalent binary number, but in reverse order.

For example, the decimal number 210 could be converted into binary as shown in Figure 7.

Figure 7

210 ÷ 2 = 105 remainder 0
105 ÷ 2 = 52 remainder 1
52 ÷ 2 = 26 remainder 0
26 ÷ 2 = 13 remainder 0
13 ÷ 2 = 6 remainder 1
6 ÷ 2 = 3 remainder 0
3 ÷ 2 = 1 remainder 1
1 ÷ 2 = 0 remainder 1


The sequence 0, 1, 0, 0, 1, 0, 1, 1 which would be output by this process is the reverse of the binary equivalent of 210 which is 11010010.

What you need to do

Task 1

Write a program that will perform the conversion process described above. The program should display a suitable prompt asking the user to input a decimal number to convert and then output the bits of the binary equivalent of the decimal number in reverse order.

Task 2

Improve the program so that the bits are output in the correct order, e.g. for 210 the output would be 11010010.

Task 3

Test the program works by entering the value 210.