C# program to convert decimal to binary

C# program to convert decimal to binary – Learn how to convert decimal Numbers (from base 10) to binary Numbers (to base 2)  with Example Programs.

Decimal Number -Decimal number is a base 10 number , it ranges from 0 to 9. Any combination of digits is decimal number such as 7, 15, 4, 0, 2 etc.
Binary Number– Binary number is a base 2 number because it is either 0 or 1. Any combination of 0 and 1 is binary number such as 111, 1111, 100, 0,10 etc.

Decimal to Binary Conversion Algorithm

Step 1: Divide the number by 2  and store the remainder in array
Step 2: Repeat the step 2 until number is greater than 0

Example Program-1 to print upto a 8 bit binary number.

 class Program
    {
        static void Main(string[] args)
        {
            int n=7, k;
            for (int c = 8; c >= 0; c--)
            {
                k = n >> c;

                if ((k & 1) >0)
                    Console.Write("1");
                else
                    Console.Write("0");
            }
        }
    }

Output
000000111

Example Program-2 to store 8 bit binary number into a string.

class Program
    {
        static void Main(string[] args)
        {
          // n  is the decimal number
            int n=7, k,i=0;
            char[] bin = new char[8]; 
            for (int c = 7; c >= 0; c--)
            {
                k = n >> c;

                if ((k & 1) > 0)
                    bin[i] = '1';
                else
                    bin[i] = '0';
                i++;
            }
            Console.WriteLine("Decimal to BInary conversion");
            Console.WriteLine("Decimal NUmber = " + 7);
            Console.Write("Binary NUmber = ");
            Console.WriteLine(bin);
        }
    }

Output
Decimal to BInary conversion
Decimal NUmber = 7
Binary NUmber = 00000111

Example Program 3-Convert a decimal number to binary without using an array.

 class Program
    {
        static void Main(string[] args)
        {
            // n  is the decimal number
            int n=7, i, j, binno = 0, dn;
            dn = n;
            i = 1;

            for (j = n; j > 0; j = j / 2)
            {
                binno = binno + (n % 2) * i;
                i = i * 10;
                n = n / 2;
            }
            Console.WriteLine("The Binary of {0} is {1}.\n\n", dn, binno);
        }
    }

Output
The Binary of 7 is 111.

Example Program 4-Decimal to Binary conversion using arrays.

 class Program
    {
        static void Main(string[] args)
        {
         // number   is the decimal number
           int     number=7,cnt,i;
            int[]     bin=new int [32];

            cnt=0;              /*initialize index to zero*/
    while(number>0)
    {
        bin[cnt]=number%2;
        number=number/2;
        cnt++;
    }
 
    /*print value in reverse order*/
    Console.WriteLine("Decimal Number is {0}", number);
    Console.WriteLine("Binary value is: "); 
    for(i=(cnt-1); i>=0;i--)
        Console.Write("{0}",bin[i]);
 
  
        }
    }

Output
Decimal Number is 0
Binary value is:
111

Related Posts