How to convert Decimal to Binary using C#?

Converting decimal numbers to binary in C# can be accomplished using the division and modulus operators. This process involves repeatedly dividing the decimal number by 2 and collecting the remainders, which form the binary representation when reversed.

The algorithm works by dividing the decimal value by 2, storing the remainder (which is always 0 or 1), and continuing until the quotient becomes 0. The binary representation is formed by reading the remainders in reverse order.

Algorithm

The conversion process follows these steps −

  1. Divide the decimal number by 2

  2. Store the remainder (0 or 1)

  3. Update the decimal number to the quotient

  4. Repeat until the quotient becomes 0

  5. Reverse the collected remainders to get the binary result

Decimal 48 to Binary Conversion 48 ÷ 2 = 24 R: 0 24 ÷ 2 = 12 R: 0 12 ÷ 2 = 6 R: 0 6 ÷ 2 = 3 R: 0 3 ÷ 2 = 1 R: 1 1 ÷ 2 = 0 R: 1 Remainders: 0, 0, 0, 0, 1, 1 Reversed: 110000

Using Manual Division Method

Example

using System;

class DecimalToBinary {
   static void Main(string[] args) {
      int decVal = 48;
      int val;
      string remainders = "";
      
      Console.WriteLine("Decimal = {0}", decVal);
      
      while (decVal >= 1) {
         val = decVal / 2;
         remainders += (decVal % 2).ToString();
         decVal = val;
      }
      
      string binary = "";
      for (int i = remainders.Length - 1; i >= 0; i--) {
         binary += remainders[i];
      }
      
      Console.WriteLine("Binary = {0}", binary);
   }
}

The output of the above code is −

Decimal = 48
Binary = 110000

Using Built-in Convert.ToString() Method

Example

using System;

class DecimalToBinaryBuiltIn {
   static void Main(string[] args) {
      int decimal1 = 48;
      int decimal2 = 25;
      int decimal3 = 100;
      
      Console.WriteLine("Decimal {0} = Binary {1}", decimal1, Convert.ToString(decimal1, 2));
      Console.WriteLine("Decimal {0} = Binary {1}", decimal2, Convert.ToString(decimal2, 2));
      Console.WriteLine("Decimal {0} = Binary {1}", decimal3, Convert.ToString(decimal3, 2));
   }
}

The output of the above code is −

Decimal 48 = Binary 110000
Decimal 25 = Binary 11001
Decimal 100 = Binary 1100100

Using Recursive Approach

Example

using System;

class RecursiveBinary {
   static string DecimalToBinary(int n) {
      if (n == 0) return "0";
      if (n == 1) return "1";
      
      return DecimalToBinary(n / 2) + (n % 2);
   }
   
   static void Main(string[] args) {
      int[] numbers = {48, 15, 7, 32};
      
      foreach (int num in numbers) {
         Console.WriteLine("Decimal {0} = Binary {1}", num, DecimalToBinary(num));
      }
   }
}

The output of the above code is −

Decimal 48 = Binary 110000
Decimal 15 = Binary 1111
Decimal 7 = Binary 111
Decimal 32 = Binary 100000

Comparison of Methods

Method Complexity Performance Best For
Manual Division Medium Good Learning algorithm
Convert.ToString() Low Best Production code
Recursive Medium Fair Functional approach

Conclusion

Converting decimal to binary in C# can be achieved through multiple approaches: manual division with remainders, the built-in Convert.ToString() method, or recursive functions. The Convert.ToString() method is most efficient for production use, while manual implementation helps understand the underlying algorithm.

Updated on: 2026-03-17T07:04:35+05:30

457 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements