# C# Program to Convert Decimal to Binary with Examples

Introduction:

In this article, we will learn how to write a C# program to convert decimal to binary with examples.

Whether you’re a beginner eager to grasp the basics or an experienced developer looking to refresh your knowledge, this guide is tailored for you.

## Section 1: The Basics of Binary and Decimal Systems

Before diving into the code, let’s briefly review the binary and decimal numbering systems. In the decimal system, we use base 10 with digits ranging from 0 to 9.

Binary, on the other hand, operates in base 2 and only uses the digits 0 and 1, making it a crucial concept in computer science.

### C# Code Example 1: Using Division and Remainder:

In the following code example, We will write a C# program to convert decimals to binary using division and reminder approach.

``````using System;

class DecimalToBinaryConverter
{
static void Main()
{
int decimalNumber = 63;
Console.WriteLine(\$"Decimal: {decimalNumber}  Binary: {ConvertDecimalToBinary(decimalNumber)}");
}

static string ConvertDecimalToBinary(int decimalNumber)
{
if (decimalNumber == 0)
return "0";

int remainder;
string binaryResult = "";

while (decimalNumber > 0)
{
remainder = decimalNumber % 2;
binaryResult = remainder.ToString() + binaryResult;
decimalNumber /= 2;
}

return binaryResult;
}
}
``````

Output:

``Decimal: 63  Binary: 111111``

Code Explanation:

• In the above code example, The `ConvertDecimalToBinary()` method takes an integer as input and returns its binary representation as a string.
• We use a while loop to repeatedly divide the decimal number by 2 until it becomes 0.
• The remainder of each division is appended to the binary result to display it to user.

### Example 2: Simple Conversion using Built-in Function

``````using System;

class DecimalToBinaryConverter
{
static void Main()
{
Console.Write("Enter a decimal number: ");

// Using Convert.ToString() with base 2 for binary conversion
string binaryRepresentation = Convert.ToString(decimalNumber, 2);

Console.WriteLine(\$"Binary equivalent: {binaryRepresentation}");
}
}
``````

Output:

``````Enter a decimal number: 42
Binary equivalent: 101010``````

Code Explanation:

In this example, we take user input for a decimal number and use `Convert.ToString()` with base 2 to convert it to binary and then display the result.

### C# Code Example 3: Using Bitwise Shift Operators:

In the following code example, We will write a C# program to convert decimals to binary using Bitwise Shift Operators.

``````using System;

class DecimalToBinaryConverter
{
static void Main()
{
while (true)
{
Console.Write("Enter a decimal number (or type 'exit' to quit): ");

if (input.ToLower() == "exit")
break;

if (int.TryParse(input, out int decimalNumber))
{
string binaryRepresentation = ConvertDecimalToBinary(decimalNumber);
Console.WriteLine(\$"Decimal: {decimalNumber}  Binary: {binaryRepresentation}");
}
else
{
Console.WriteLine("Invalid input. Please enter a valid decimal number or type 'exit' to quit.");
}
}
}

static string ConvertDecimalToBinary(int decimalNumber)
{
const int bits = 32;
char[] binaryResult = new char[bits];

for (int i = 0; i < bits; i++)
{
binaryResult[i] = (char)('0' + (decimalNumber >> (bits - 1 - i) & 1));
}

return new string(binaryResult).TrimStart('0');
}
}
``````

Output:

``````Enter a decimal number (or type 'exit' to quit): 63
Decimal: 63  Binary: 111111
Enter a decimal number (or type 'exit' to quit): 42
Decimal: 42  Binary: 101010
Enter a decimal number (or type 'exit' to quit): exit``````

Code Explanation:

1. Main Method: This is the entry point of the program. It initializes a decimal number (`decimalNumber`) with the value 42.
2. Console Output: It displays the original decimal number and its binary representation using `Console.WriteLine`.
3. ConvertDecimalToBinary Method: This method takes an integer (decimal number) as input and returns its binary representation as a string.
4. Constant Declaration: The `bits` constant is set to 32, representing the number of bits in the binary representation. This assumes a 32-bit integer.
5. Binary Result Array: An array `binaryResult` is created to store the individual bits of the binary representation.
6. Bitwise Iteration: A `for` loop iterates through each bit position in the binary representation.
7. Bitwise Operations: Inside the loop, bitwise shift (`>>`) and AND (`&`) operators are used to extract individual bits from the decimal number. The result is then stored in the `binaryResult` array.
8. String Conversion: The binary array is converted to a string, and leading zeros are trimmed to create a cleaner binary representation.

Overall, We have written the above program to provide a simple demonstration of converting a decimal number to binary using bitwise operations in C#.

### Example 4: Recursive Approach

``````using System;

class DecimalToBinaryConverter
{
static void Main()
{
Console.Write("Enter a decimal number: ");

// Recursive binary conversion
string binaryRepresentation = DecimalToBinary(decimalNumber);

Console.WriteLine(\$"Binary equivalent: {binaryRepresentation}");
}

static string DecimalToBinary(int decimalNumber)
{
if (decimalNumber == 0)
return "0";
else
return DecimalToBinary(decimalNumber / 2) + (decimalNumber % 2);
}
}
``````

Output:

``````Enter a decimal number: 42
Binary equivalent: 0101010``````

Code Explanation:
This example demonstrates a recursive approach for Decimal to binary conversion.

The `DecimalToBinary` method uses recursion to convert the decimal number to binary. It keeps dividing the number by 2 and appending the remainders until the base case (when the decimal number becomes 0) is reached.

• Recursive Approach: The `DecimalToBinary()` method uses recursion for binary conversion.
• Base Case: The function returns “0” when the decimal number becomes 0.

## Conclusion:

In this article, we’ve explored C# code examples for converting decimal to binary using the inbuilt function, recursive, and bitwise shift approaches.

Recommended Articles: