Convert a denary number to a binary number.
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:
x % yReturns the modulus (remainder) of dividing x by y. % is not percentage in C#: it is the modulo operator, which calculates the remainder left after a division.
E.g. seven sweets divided by two people is exactly three sweets each with one left over. The one remaining is the modulus, one.
7 % 2 == 1
x / y (integer division)When both operands are int, / is integer (floor) division. It returns the integer quotient with no fractional part — any decimal places are discarded. E.g. 7 / 2 when both are int is 3, not 3.5.
To force floating-point division, make at least one operand a double: 7.0 / 2 is 3.5.
Math.Pow(x, y)Returns, as a double, x raised to the power of y. E.g. Math.Pow(3, 5) is 243. It is the same as 3 * 3 * 3 * 3 * 3.
x.ToString()Converts a numeric value x to its string representation. 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) while "6" is 00110110.
decimal x = Convert.ToDecimal(y);Converts the number y into a decimal x. double and decimal are two different ways of storing a number with a fractional component:
double (floating point) holds about 16 digits, is faster, and uses 8 bytes of memory;decimal (fixed point) holds about 29 digits, is slower, and uses 16 bytes of memory.Use a double when performance is more important than precision (e.g. graphics and games), and a decimal when avoiding rounding errors is more important than performance (e.g. financial calculations).
Math.Abs(y)Returns the absolute value of y. Absolute values are always positive. E.g. Math.Abs(-5) is 5. Math.Abs(5) is also 5. A useful way to turn a negative number into a positive number.
Math.Round(y, z)Returns y rounded to z decimal places. Both extra parameters are optional: with no decimal-places argument it rounds to the nearest whole number, and an optional third argument sets the rounding strategy.
By default rounding uses MidpointRounding.ToEven (banker’s rounding), where a midpoint value is rounded to the nearest even number, so 6.5 and 5.5 both round to 6. Pass MidpointRounding.AwayFromZero to round midpoints away from zero instead.
Math.Ceiling(y)Returns y rounded up to the nearest integer (as a double).
Math.Floor(y)Returns y rounded down to the nearest integer (as a double).
Math.Sqrt(y)Returns, as a double, the square root of y.
Math.PIThe constant π, 3.1415926535897931.
Questions to think about with this program to check your understanding:
Why is the variable binary concatenated to the same variable binary inside the loop? I.e. why can’t the line read binary = remainder.ToString() instead?
Because the new digit calculated from the division must be added on to the existing binary sequence that has been previously calculated.
What happens when number and 2 are both int and we write number / 2? How would you write the division if number were a double and you wanted only the integer part?
When both operands are int, / performs integer division — the fractional part is discarded. If number were a double you would cast: (int)(number / 2).
Change the program so that it:
den_to_oct that converts any positive integer from denary to octal. Where binary is base 2, octal is base 8.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.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
// -------------------------
// Main program
// -------------------------
---
public static void Main(string[] args)
{
---
Console.WriteLine("Enter the denary number to convert:");
int number = Convert.ToInt32(Console.ReadLine());
---
Console.WriteLine("Binary: " + den_to_bin(number));
Console.WriteLine("Octal: " + den_to_oct(number));
Console.WriteLine("Hexadecimal: " + den_to_hex(number));
}
den_to_bin
// Function to convert a denary number to binary using repeated division by 2
---
static string den_to_bin(int number)
{
---
string binary = "";
while (number > 0)
{
---
// Return remainder using modulus 2
int remainder = number % 2;
---
// Cast remainder to a string to concatenate to binary sequence
binary = Convert.ToString(remainder) + binary;
---
// Integer divide the number by 2
number = number / 2;
---
}
---
return binary;
}
den_to_oct
// Function to convert a denary number to octal using repeated division by 8
---
static string den_to_oct(int number)
{
---
string octal = "";
while (number > 0)
{
---
// Return remainder using modulus 8
int remainder = number % 8;
---
// Cast remainder to a string to concatenate to octal sequence
octal = Convert.ToString(remainder) + octal;
---
// Integer divide the number by 8
number = number / 8;
---
}
---
return octal;
}
den_to_hex
// Function to convert a denary number to hexadecimal using repeated division by 16
---
static string den_to_hex(int number)
{
---
string hex = "";
while (number > 0)
{
---
// Return remainder using modulus 16
int remainder = number % 16;
---
string digit = Convert.ToString(remainder);
---
// Resolve letters A-F
switch (remainder)
{
---
case 10: digit = "A"; break;
---
case 11: digit = "B"; break;
---
case 12: digit = "C"; break;
---
case 13: digit = "D"; break;
---
case 14: digit = "E"; break;
---
case 15: digit = "F"; break;
---
}
---
// Concatenate hex sequence
hex = Convert.ToString(digit) + hex;
---
// Integer divide the number by 16
number = number / 16;
---
}
---
return hex;
}