Convert a denary number to a binary number.
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:
The new commands used in this program and others that may be useful. Select them below to learn more:
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.
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. |
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 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 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 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 is assigned the value of y rounded up to the nearest integer.
x is assigned the value of y rounded down to the nearest integer.
x is assigned as a double that is the square root of y.
x is assigned to be the constant pi as 3.1415926535897931.
Questions to think about with this program to check your understanding:
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?
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.
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?
(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.
Change the program so that it:
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
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.
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