The Program in C# Sharp program that takes a decimal number as input and displays its equivalent in binary form is given below:
using System;
namespace DecimalToBinary
{
class Program
{
static void Main(string[] args)
{
Console.Write("Hello Codeauri Family ,enter a decimal number to convert them into binary form: ");
int decimalNumber = int.Parse(Console.ReadLine());
string binary = Convert.ToString(decimalNumber, 2);
Console.WriteLine("Okay, the Binary equivalent is : " + binary);
Console.ReadLine();
}
}
}
Output:
Hello Codeauri Family ,enter a decimal number to convert them into binary form: [?1h=[6n[6n10
Okay, the Binary equivalent is : 1010
Pro-Tips💡
Here are the step by step execution of above program:
- The
using System;
directive at the top of the code is a using directive that includes theSystem
namespace in the program. This namespace includes theConsole
class, which provides methods for reading from and writing to the console. - The next line defines the namespace for the program. A namespace is a container for classes and other code elements in C#, and it helps to organize and structure the code. In this case, the namespace is called
DecimalToBinary
. - Inside the namespace, a class called
Program
is defined. This class contains the main logic of the program. - The
Main
method is the entry point of the program. This method is executed when the program starts. - Within the
Main
method, theConsole.Write
method is called to display a message on the console asking the user to enter a decimal number. - The
Console.ReadLine
method is then used to read the user’s input, which is stored in thedecimalNumber
variable as a string. Theint.Parse
method is used to convert the string to an integer. - The
Convert.ToString
method is then called with thedecimalNumber
and the base 2 (binary) as arguments, to convert the decimal number to its binary equivalent. The binary equivalent is stored in thebinary
string variable. - The binary equivalent is then displayed on the console using the
Console.WriteLine
method, along with a message. - Finally, the
Console.ReadLine
method is called to prevent the console from closing immediately after the program runs.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.