Decimal (base 10) is the everyday number system. Binary (base 2) uses only bits 0 and 1. Conversion means rewriting a decimal integer as a string of bits.
Remember
Divide by 2 → remainder is the next bit (LSB first)
Reverse the remainders → MSB … LSB binary string
Example: 13
13 ÷ 2 = 6 rem 1
6 ÷ 2 = 3 rem 0
3 ÷ 2 = 1 rem 1
1 ÷ 2 = 0 rem 1
Remainders (LSB→MSB): 1 0 1 1
Binary (MSB→LSB): 1101
Decimal
Binary
Check
0
0
special case
13
1101
8+4+1
42
101010
32+8+2
Approach
How to Convert Decimal to Binary in C#
Four clear steps — then pick a remainder loop or the built-in formatter.
Handle n == 0 by returning "0".
While n > 0, record n % 2 and set n = n / 2.
Reverse the collected remainders (or build the string from the front).
Or call Convert.ToString(n, 2) for a one-line answer.
Method
Idea
Time
Best for
Remainder loop
Divide by 2; reverse remainders
O(log n)
Interviews, learning place values
Convert.ToString(n, 2)
Built-in base-2 formatting
O(log n)
Production code, short answers
Pseudocode
Pseudocode
function DecimalToBinary(n):
if n == 0:
return "0"
bits = empty list
while n > 0:
bits.append(n mod 2)
n = n / 2
reverse bits
return join(bits)
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Operation
Divide by 2; remainder is a bit
First remainder
LSB (rightmost bit)
Zero
Return "0" — do not skip
Built-in
Convert.ToString(n, 2)
Negatives
Usually restrict to n >= 0 in tutorials
Classic check
13 → 1101
Try it
Live Preview
Enter a nonnegative integer to see the remainder steps and the binary result.
Nonnegative integers (capped at 2147483647). Tap a chip or type a value.
Start with the remainder loop so you can explain every bit. Then use Convert.ToString for a short production-style answer.
Example 1 — Remainder Loop
Collect n % 2 values, then reverse them into a binary string.
C#
using System;
using System.Collections.Generic;
using System.Text;
class Program
{
static string DecimalToBinary(int n)
{
if (n < 0)
{
throw new ArgumentException("Use a nonnegative integer.");
}
if (n == 0)
{
return "0";
}
List<int> bits = new List<int>();
while (n > 0)
{
bits.Add(n % 2);
n /= 2;
}
StringBuilder result = new StringBuilder();
for (int i = bits.Count - 1; i >= 0; i--)
{
result.Append(bits[i]);
}
return result.ToString();
}
static void Main()
{
int number = 13;
Console.WriteLine(number + " → " + DecimalToBinary(number));
}
}
Output
13 → 1101
How It Works
Reject negatives for this beginner version; return "0" immediately when n is zero.
Each n % 2 is the next bit from the right (LSB first).
n /= 2 shifts toward higher bits until the quotient becomes 0.
Append bits from the end of the list so the string reads MSB to LSB.
For 13, remainders 1,0,1,1 reverse to 1101.
Example 2 — Convert.ToString
Let the framework format the integer as base 2 after a quick nonnegative check.
C#
using System;
class Program
{
static string DecimalToBinary(int n)
{
if (n < 0)
{
throw new ArgumentException("Use a nonnegative integer.");
}
return Convert.ToString(n, 2);
}
static void Main()
{
Console.WriteLine(DecimalToBinary(13)); // 1101
Console.WriteLine(DecimalToBinary(42)); // 101010
Console.WriteLine(DecimalToBinary(0)); // 0
}
}
Output
1101
101010
0
How It Works
Keep the same nonnegative rule as the manual method.
Convert.ToString(n, 2) means “write n using base 2.”
Zero is handled correctly and returns "0".
The result matches the remainder algorithm without storing a list.
Use the loop in interviews when you must show the math; use this built-in in real projects.
Edge Cases & Pitfalls
Check these before calling the solution done.
n = 0
Return "0"
A bare while (n > 0) loop produces an empty string otherwise.
Reverse
LSB comes first
Printing remainders in collection order yields the bits backwards.
Negatives
Define the policy
Tutorials usually require n >= 0. Two’s complement needs a fixed width.
Width
Minimal vs padded
Default output drops leading zeros. Pad only when the problem asks for 8/16/32 bits.
Analysis
Time and Space Complexity
Program
Time
Extra space
Remainder loop
O(log n)
O(log n) for the bit list
Convert.ToString(n, 2)
O(log n)
O(log n) for the result string
Remember
Key Takeaways
Method: divide by 2; remainders are bits (LSB first).
Reverse: print from last remainder to first for normal binary.
Built-in:Convert.ToString(n, 2) after you know the algorithm.
Test set: 0 → 0; 13 → 1101; 42 → 101010.
One line: collect n % 2 remainders and reverse them — or call Convert.ToString(n, 2).
Frequently Asked Questions
Repeatedly divide by 2 and collect remainders (LSB first), then reverse them — or call Convert.ToString(n, 2) for nonnegative integers.
The first remainder is the least significant bit. Printing from last remainder to first gives the usual left-to-right binary string.
Handle n == 0 separately and return "0". A while (n > 0) loop would otherwise produce an empty string.
It formats the integer n as a base-2 string. It is the idiomatic built-in after you understand the algorithm.
Both methods are O(b), where b is the number of output bits — about O(log n) for positive n.