Convert a denary number to a binary number.

Denary to binary

⭐⭐⭐

Try

Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

The new commands used in this program and others that may be useful. Select them below to learn more:

x % y

x is assigned to be the modulus (remainder of division) of x by y.

% is not percentage in Python, it is the modulo operator that calculates the remainder from a division, known as the modulus. E.g. seven sweets divided by two people is exactly two sweets each with one left over. The one remaining is the modulus, one.

7 % 2 = 1.

x // y

Is an integer (floor) division operator. It returns the result of a division as an integer with no fractional part. Any decimal places are discarded. The number is also known to be truncated. E.g. 7 / 2 = 3.5 but 7 // 2 = 3.

This can also be achieved with:

int(7 / 2)

x ** y

Is exponentiation, or to the power of. E.g. 3 ** 5 = 243. It is the same as 3 * 3 * 3 * 3 * 3.

x = str(y)

Casts (converts) the number y into the string x. Remember that the number 6 and the string “6” are not the same because numbers and strings are held in memory using different binary sequences. 6 is 110 (assuming unsigned binary) and “6” is 00110110.

x = abs(y)

x is assigned the absolute value of y. Absolute values are always positive. E.g. abs(-5) is 5. abs(5) is also 5. The abs function is a useful way to turn a negative number into a positive number.

x = round(y, z)

x is assigned the rounded value of y to z decimal places using the 0.5 rule.

x = math.ceil(y)

x is assigned the value of y rounded up to the nearest integer. Requires import math command to access this command.

x = math.floor(y)

x is assigned the value of y rounded down to the nearest integer. Requires import math command to access this command.

x = math.sqrt(y)

x is assigned the square root of y. Requires import math command to access this command.

x = math.pi

x is assigned to be the constant pi.

Investigate

Questions to think about with this program to check your understanding:

Relation question

Why is the variable binary concatenated to the same variable binary in line 10? I.e. why can’t the line read binary = str(remainder) instead?

Reveal answer

Because the new digit calculated from the division must be added on to the end of the existing binary sequence that has been previously calculated.

Approach question

What is the difference between using number / 2 and number // 2 in line 11? Could the programmer use int(number / 2) instead?

Reveal answer

number / 2 means divide the number by two whereas number // 2 means divide the number by two and only keep the integer part of the result, known as integer or floor division.

Yes, the programmer could cast the result of the division (which is always a float) to an integer instead. It is just a matter of preference.

Make

Change the program so that it:

  1. Includes a function called den_to_oct that converts any positive integer from denary to octal. Where binary is base 2, octal is base 8.
  2. Includes a function called den_to_hex that converts any positive integer from denary to the base 16, hexadecimal. The number 10 is the letter A in hexadecimal, 11 is B, 12 is C, 13 is D, 14 is E and 15 is F.
  3. Update the main program to convert the number input into binary. octal and hexadecimal.

Typical inputs and outputs from the program would be:

Enter the denary number to convert: 10
Binary: 1010
Octal: 12
Hexadecimal: A

Enter the denary number to convert: 23
Binary: 10111
Octal: 27
Hexadecimal: 17

Enter the denary number to convert: 75
Binary: 1001011
Octal: 113
Hexadecimal: 4B