C# Program To Convert Decimal To Binary - C# Tutorial
C# Course / Programs / C# Program To Convert Decimal To Binary

C# Program To Convert Decimal To Binary

BLUF: Mastering C# Program To Convert Decimal To Binary is essential for building robust applications with the .NET ecosystem. This tutorial provides clear explanations and practical examples to help you understand and apply this C# concept.
Enterprise Development Tip: C# Program To Convert Decimal To Binary

C# is a powerful, modern language for enterprise solutions. Discover how C# Program To Convert Decimal To Binary enhances your development workflow in the guide below.

We have the ability to transform any decimal numeral (base-10 (0 through 9)) into a binary numeral (base-2 (0 or 1)) using a C# program.

Decimal Number

A decimal number is classified as a base 10 number due to its range from 0 to 9, encompassing a total of 10 digits. Any sequence of these digits, like 223, 585, 192, 0, or 7, constitutes a decimal number.

Binary Number

A binary number operates on a base 2 system, encompassing only the digits 0 and 1. Any sequence containing a combination of these two digits forms a binary number, like 1001, 101, 11111, or 101010.

Let's examine some binary representations for decimal numbers.

Decimal Binary
1 0
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010

Decimal to Binary Conversion Algorithm

Step 1: Divide the number by 2 using the % (modulus operator) and save the remainder in an array.

Step 2: Divide the value by 2 using the / (division operator).

Repeat the second step iteratively until the numerical value exceeds zero.

Let's explore the C# demonstration on converting a decimal number to binary.

Example

Example

using System;

  public class ConversionExample

   {

     public static void Main(string[] args)

      {

       int  n, i;     

       int[] a = new int[10];   

       Console.Write("Enter the number to convert: ");  

       n= int.Parse(Console.ReadLine());   

       for(i=0; n>0; i++)    

        {    

         a[i]=n%2;    

         n= n/2;  

        }    

       Console.Write("Binary of the given number= ");    

       for(i=i-1 ;i>=0 ;i--)    

       {    

        Console.Write(a[i]);    

       }               

      }

  }

Output:

Output

Enter the number to convert:10

Binary of the given number= 1010

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience