C# For Dummies

Chapter 8. Numeral Systems

1. Convert the numbers 151, 35, 43, 251, 1023 and 1024 to the binary numeral system.

Guidelines: You can check your results with the help of the Windows built-in calculator, which supports numeral systems in "Programmer" mode. The results are: 10010111, 100011, 101011, 11111011, 1111111111 and 10000000000

Solution:
static void Main(string[] args)
{
    Console.WriteLine("150 to binary {0}.", Convert.ToString(150, 2));
    Console.WriteLine("35 to binary {0}.", Convert.ToString(35, 2));
    Console.WriteLine("43 to binary {0}.", Convert.ToString(43, 2));
    Console.WriteLine("251 to binary {0}.", Convert.ToString(251, 2));
}

2. Convert the number 1111010110011110(2) to hexadecimal and decimal numeral systems.

Guidelines: Like the previous exercise. Result: F59E(16) and 62878(10).

Solution:
static void Main(string[] args)
{
    Console.WriteLine("1111010110011110 to decimal is {0}.",
        Convert.ToInt64("1111010110011110", 2));

    Console.WriteLine("1111010110011110 to hexadecimal is {0}.",
        Convert.ToInt64("1111010110011110", 2).ToString("X"));
}

3. Convert the hexadecimal numbers FA, 2A3E, FFFF, 5A0E9 to binary and decimal numeral systems.

Guidelines: Like the previous exercise. The results are: FA(16) = 250(10) = 11111010(2), 2A3E(16) = 10814(10) = 10101000111110(2), FFFF(16) = 65535(10) = 1111111111111111(2) and 5A0E9(16) = 368873(10) = 1011010000011101001(2).

Solution:
static void conversion(string value)
{
    Console.WriteLine("{0} to decimal is {1}.",
        value, Convert.ToInt32(value, 16));
    Console.WriteLine("{0} to decimal is {1}.\n", value,
        Convert.ToString(Convert.ToInt32(value, 16), 2));    
}

static void Main(string[] args)
{
    conversion("2A3E");
    conversion("FA");
    conversion("FFFF");
    conversion("5A0E9");
}

4. Write a program that converts a decimal number to binary one.

Guidelines: The rule is "divide by 2 and concatenate the remainders in reversed order". For division with a remainder we use the % operator. You can cheat by invoking Convert.ToString(numDecimal, 2) .

Solution:
static void Main(string[] args)
{
    Console.Write("Enter decimal number: ");
    int deci = Int32.Parse(Console.ReadLine());
    Console.WriteLine("{0} to binary is {1}.",
        deci, deci.ToString(deci, 2));
}

5. Write a program that converts a binary number to decimal one

Guidelines: Start with a sum of 0. Multiply the right-most bit with 1 and add it to the sum. Multiply the next bit on the left by 2 and add it to the sum. Multiply the next bit on the left by 4, the next by 8 and so on. You can cheat by invoking Convert.ToInt32(binaryNumAsString, 2)

Solution:
static void Main(string[] args)
{
    Console.Write("Enter binary number: ");
    string binary = Console.ReadLine();
    Console.WriteLine("{0} to decimal is {1}.",
        binary, Convert.ToInt64(binary, 2));
}

6. Write a program that converts a decimal number to hexadecimal one.

Guidelines: The rule is "divide by the base of the system (16) and concatenate the remainders in reversed order". A logic that gets a hexadecimal digit (0…F) by decimal number (0…15) should also be implemented. You can cheat by invoking num.ToString("X").

Solution:
static void Main(string[] args)
{
    Console.Write("Enter decimal number: ");
    int deci = Int32.Parse(Console.ReadLine());
    Console.WriteLine("{0} to hexadecimal is {1}.",
        deci, deci.ToString("X"));
}

7. Write a program that converts a hexadecimal number to decimal one.

Guidelines: Start with a sum of 0. Multiply the right-most digit with 1 and add it to the sum. Multiply the next digit to the left by 16 and add it to the sum. Multiply the next digit by 16*16, the next by 16*16*16 and so on. You can cheat by invoking Convert.ToInt32(hexNumAsString, 16) .

Solution:
static void Main(string[] args)
{
    Console.Write("Enter hexadecimal number: ");
    string hexa = Console.ReadLine();
    Console.WriteLine("{0} to decimal is {1}.",
        hexa, Convert.ToInt32(hexa, 16));
}

8. Write a program that converts a hexadecimal number to binary one.

Guidelines: Use the fast method for transitioning between hexadecimal and binary numeral system (each hexadecimal digit turns to 4 binary bits).

Solution:
static void Main(string[] args)
{
    Console.Write("Enter hexadecimal number: ");
    string hexa = Console.ReadLine();
    Console.WriteLine("{0} to binary is {1}.",
        hexa, Convert.ToString(Convert.ToInt32(hexa, 16), 2));
}

9. Write a program that converts a binary number to hexadecimal one.

Guidelines: Use the fast method for transitioning from binary to hexadecimal numeral system (each 4 binary bits correspond to a hexadecimal digit).

Solution:
static void Main(string[] args)
{
    Console.Write("Enter binary number: ");
    string binary = Console.ReadLine();
    Console.WriteLine("{0} to hexadecimal is {1}.",
        binary, Convert.ToInt32(binary, 2).ToString("X");
}

10. Write a program that converts a binary number to decimal using the Horner scheme.

Guidelines: Directly apply the Horner scheme.

Solution:
Link
11. Write a program that converts Roman digits to Arabic ones.

Guidelines: Scan the digits of the Roman number from left to right and add them up to a sum, which is initialized with a 0. When processing each Roman digit, take it with a positive or negative sign, depending on the digit after it (whether it has a bigger or smaller decimal value).

Solution:
Link
12. Write a program that converts Arabic digits to Roman ones.

Guidelines: We have exactly the same correspondence for the numbers 10, 20, …, 90 with their Roman representation "X", "L" and "C". The same is valid for the numbers 100, 200, …, 900 and their Roman representation with "C", "D" and "M" and so on.
We are now ready to convert the number N into the Roman numeral system. It must be in the range [1…3999], otherwise we should report an error. First we separate the thousands (N / 1000) and replace them with their Roman counterpart. After that we separate the hundreds (N / 100) % 10) and separate them with their Roman counterpart and so on.

Solution:
Link
13. Write a program that by given N, S, D converts the number N from an S-based numeral system to a D based numeral system.

Guidelines: You can convert first from S-based system to decimal number and then from decimal number to D-based system.

Solution:
Link

← Back