Denary to binary

Denary to binary

Convert a denary number to a binary number.

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

Command summary

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

x % y

This 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


Success Criteria

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

Help

Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.

Evaluate


Run your code to check that your program has met the success criteria and produces the required output.

Enter the denary number to convert: 10

Binary: 1010

Octal: 12

Hexadecimal: A

Enter the denary number to convert: 95

Binary: 1011111

Octal: 137

Hexadecimal: 5F

Enter the denary number to convert: 101

Binary: 1100101

Octal: 145

Hexadecimal: 65

Enter the denary number to convert: 253

Binary: 11111101

Octal: 375

Hexadecimal: FD

Craig'n'Dave logo

Craig ‘n’ Dave

Bett Awards 2024 Finalist