Hexadecimal And Binary - C# Programming Exercise

In this C# exercise, you are asked to write a program that prompts the user for a number and displays it in both hexadecimal and binary. The program should continue repeating this process until the user enters a 0.

The program should convert the entered number to its hexadecimal and binary equivalents using the appropriate C# functions. The hexadecimal format is commonly used in programming to represent values compactly, while the binary system is essential for understanding how computers work and how data is represented internally.

Through this exercise, you will learn how to handle conversions between different numeric bases, such as binary (base 2) and hexadecimal (base 16), which is crucial for low-level programming and working with memory operations. This type of exercise will also help you practice using loops and conditionals to control the program flow based on user input.

 Category

Basic Data Types

 Exercise

Hexadecimal And Binary

 Objective

Write a C# program to ask the user for a number an display it both in hexadecimal and binary. It must repeat until the user enters 0.

 Write Your C# Exercise

using System;  // Import the System namespace for basic functionality

class Program  // Define the main class
{
    static void Main()  // The entry point of the program
    {
        // Infinite loop to keep asking the user for input until they enter 0
        while (true)
        {
            // Ask the user for a number
            Console.Write("Enter a number (0 to quit): ");
            int number = int.Parse(Console.ReadLine());  // Read the input and convert it to an integer

            // If the number is 0, exit the loop and end the program
            if (number == 0)
            {
                Console.WriteLine("Goodbye!");
                break;  // Exit the loop
            }

            // Display the number in hexadecimal
            Console.WriteLine($"Hexadecimal: {number:X}");

            // Display the number in binary
            Console.WriteLine($"Binary: {Convert.ToString(number, 2)}\n");
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Basic Data Types

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Binary

    In this C# exercise, you are asked to write a program that prompts the user for a decimal number and displays its equivalent in binary. The program should continue re...

  •  Conditional and boolean

    In this C# exercise, you are asked to write a program that uses the conditional operator (also known as the ternary operator) to assign a boolean variable named "bothEven...

  •  Exceptions V2

    In this C# exercise, you are asked to write a program that prompts the user for a real number and displays its square root. Additionally, the program must handle any ...

  •  Char

    This exercise in C# aims to develop a program that asks the user for three letters and displays them in reverse order. The user will input one letter at...

  •  Triangle

    This exercise in C# aims to develop a program that prompts the user for a symbol and a width, and then displays a decreasing triangle of the spec...

  •  Password as string

    This exercise in C# aims to develop a program that asks the user for their username and password (both as strings) and repeats the prompt as many...