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
C# Exercise Example
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");
}
}
}
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#.
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...
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...
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 ...
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...
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...
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...