Hexadecimal and binary Learn programming C#

Lesson:

Basic Data Types


Exercise:

Hexadecimal and binary


Objetive:

Create 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.


Code:

using System;
public class exercise68
{
    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);
    }
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.