Denary to binary

Denary to binary

Convert a denary number to a binary number.

Try


Select the button below to open the C# 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:

string x = Convert.ToString(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.

decimal x = Convert.ToDecimal(y)

Casts (converts) the number y into a decimal number x. Double and decimal are two different ways of storing a number with a fractional component. The Math functions use decimal numbers so double numbers must be cast to decimal when used in these functions. E.g.

x = Math.Pow

Feature
Double
Decimal
Precision

16 digits

29 digits

Range

±5.0 × 10^−324 to ±1.7 × 10^308

±1.0 × 10^−28 to ±7.9 × 10^28

Type

Floating point

Fixed point

Performance

Faster and uses 8 bytes of memory

Slower and uses 16 bytes of memory

Uses

When performance is more important than precision. E.g. graphics and video games.

When avoiding rounding errors is more important than performance. E.g. financial applications.

x % y

This is not percentage in C#, 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 = Math.Pow(x, y)

x is assigned to be a double that is the result of x raised to the power of y. E.g. Math.Pow(3, 5) = 243. It is the same as 3 * 3 * 3 * 3 * 3.

x = Math.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 `Math.Abs` function is a useful way to turn a negative number into a positive number.

x = Math.Round(w, y, z)

x is assigned the rounded value of w. The parameters y and z are optional. If neither are specified, this will return the value of x to the nearest whole number. The second parameter `y` specifies the number of decimal places in the output, and the third parameter `z` specifies the rounding strategy to be used. Rounding strategies include MidpointRounding.AwayFromZero. The default is MidpointRounding.ToEven, which is where midpoint values are rounded to the nearest even number. E.g. 6.5 and 5.5 both round to 6.

x = Math.Ceiling(y)

x is assigned the value of y rounded up to the nearest integer.

x = Math.Floor(y)

x is assigned the value of y rounded down to the nearest integer.

x = Math.Sqrt(y)

x is assigned as a double that is the square root of y.

x = Math.PI

x is assigned to be the constant pi as 3.1415926535897931.

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 16? I.e. why can't the line read `binary = Convert.ToString(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 `number / 2` and using `(int)number / 2` instead in line 17? Why does number / 2 work considering it may return a number with a fractional component but number was declared as an integer?

REVEAL ANSWER

(int)(number / 2) will cast the result to an integer. The decimal places are lost and the number is rounded down. This is an integer division. However, if the result is to be stored in an integer variable then number / 2 will have the same effect because any decimal places are lost when it is stored.

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

Registered in England and Wales: 10442992

VAT Number: 290 9845 58

Telephone: 01452 947500

Email: admin@craigndave.co.uk

Bett Awards 2024 Finalist