0
0
CsharpProgramBeginner · 2 min read

C# Program to Convert Decimal to Binary Number

You can convert a decimal number to binary in C# by using Convert.ToString(decimalNumber, 2) or by repeatedly dividing the number by 2 and collecting remainders.
📋

Examples

Input5
Output101
Input10
Output1010
Input0
Output0
🧠

How to Think About It

To convert a decimal number to binary, you divide the number by 2 repeatedly and record the remainder each time. These remainders, read in reverse order, form the binary number. Alternatively, you can use C#'s built-in method to convert directly.
📐

Algorithm

1
Get the decimal number input.
2
If the number is 0, return "0" as binary.
3
While the number is greater than 0, divide it by 2 and store the remainder.
4
Collect all remainders in reverse order to form the binary string.
5
Return the binary string.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        int decimalNumber = 10;
        string binary = Convert.ToString(decimalNumber, 2);
        Console.WriteLine(binary);
    }
}
Output
1010
🔍

Dry Run

Let's trace the decimal number 10 through the conversion using division and remainder.

1

Initial number

decimalNumber = 10

2

Divide by 2 and get remainder

10 / 2 = 5 remainder 0

3

Divide quotient by 2 and get remainder

5 / 2 = 2 remainder 1

4

Divide quotient by 2 and get remainder

2 / 2 = 1 remainder 0

5

Divide quotient by 2 and get remainder

1 / 2 = 0 remainder 1

6

Collect remainders in reverse

Remainders: 1 0 1 0 → binary = "1010"

QuotientRemainder
100
51
20
11
💡

Why This Works

Step 1: Using Convert.ToString

The method Convert.ToString(number, 2) converts the decimal number directly to its binary string representation.

Step 2: Manual division method

Dividing the number by 2 repeatedly and collecting remainders builds the binary digits from least significant to most significant.

Step 3: Reversing remainders

Reading the collected remainders in reverse order gives the correct binary number.

🔄

Alternative Approaches

Manual loop with StringBuilder
csharp
using System;
using System.Text;
class Program {
    static void Main() {
        int decimalNumber = 10;
        StringBuilder binary = new StringBuilder();
        int number = decimalNumber;
        if (number == 0) binary.Append('0');
        while (number > 0) {
            binary.Insert(0, number % 2);
            number /= 2;
        }
        Console.WriteLine(binary.ToString());
    }
}
This approach shows the manual process clearly but is longer than using Convert.ToString.
Using recursion
csharp
using System;
class Program {
    static void Main() {
        int decimalNumber = 10;
        Console.WriteLine(ToBinary(decimalNumber));
    }
    static string ToBinary(int n) {
        if (n == 0) return "0";
        if (n == 1) return "1";
        return ToBinary(n / 2) + (n % 2).ToString();
    }
}
Recursive method is elegant but may be less efficient for very large numbers.

Complexity: O(log n) time, O(log n) space

Time Complexity

The number of divisions by 2 is proportional to the number of binary digits, which is about log base 2 of the number.

Space Complexity

Storing the binary digits requires space proportional to the number of bits, also about log base 2 of the number.

Which Approach is Fastest?

Using Convert.ToString is fastest and simplest, while manual or recursive methods are more educational but less efficient.

ApproachTimeSpaceBest For
Convert.ToStringO(log n)O(log n)Quick and simple conversion
Manual loopO(log n)O(log n)Learning and understanding process
RecursionO(log n)O(log n)Elegant code but less efficient for large inputs
💡
Use Convert.ToString(number, 2) for a quick and reliable decimal to binary conversion in C#.
⚠️
Beginners often forget to reverse the collected remainders, resulting in an incorrect binary string.