Programming Course in C# ¡Free!

Hexadecimal and binary

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise

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

Output



Solution


using System;
public class BinaryAndHexadecimal
{
public static void Main()
{
int n;

do
{
Console.Write("Enter a number:");
n = Convert.ToInt32(Console.ReadLine());

if( n != 0 )
{
Console.Write("Hexadecimal: ");
Console.WriteLine( Convert.ToString( n, 16 ) );
Console.Write("Binary: ");
Console.WriteLine( Convert.ToString( n, 2 ) );
}
}
while (n != 0);
}
}