Basic Programming C# Examples

C# Program to Convert Decimal Number to Binary form

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:

  1. The using System; directive at the top of the code is a using directive that includes the System namespace in the program. This namespace includes the Console class, which provides methods for reading from and writing to the console.
  2. 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.
  3. Inside the namespace, a class called Program is defined. This class contains the main logic of the program.
  4. The Main method is the entry point of the program. This method is executed when the program starts.
  5. Within the Main method, the Console.Write method is called to display a message on the console asking the user to enter a decimal number.
  6. The Console.ReadLine method is then used to read the user’s input, which is stored in the decimalNumber variable as a string. The int.Parse method is used to convert the string to an integer.
  7. The Convert.ToString method is then called with the decimalNumber and the base 2 (binary) as arguments, to convert the decimal number to its binary equivalent. The binary equivalent is stored in the binary string variable.
  8. The binary equivalent is then displayed on the console using the Console.WriteLine method, along with a message.
  9. 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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X