# 4 different C# programs to convert a decimal number to binary

## C# program to convert decimal to binary:

This post will show you how to convert a decimal value to binary in C#. Decimal number system is base 10 number system and binary number system is base 2 number system. The program will take a decimal number as input from the user and it will convert and it will convert it to binary and print it to the user.

### Decimal number system:

Decimal is a base 10 number system. It uses 10 different digits to represent a number: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. It is the most popular number system.

### Binary number system:

Binary is a base 2 number system. It uses 1 and 0 to represent a number in binary. Each digit is known as bit in binary.

To convert a decimal value to binary, we will follow the following algorithm:

### Algorithm to convert Decimal to Binary:

1. Divide the number by 2 and keep the remainder in an array.
2. Change the number to the quotient.
3. Keep dividing the number by 2 until the number is greater than 0.
4. Print the content of the array in reverse.

For example, if we want to convert 21 to binary, we will have to follow the following steps:

• 21/2, remainder is 1, quotient is 10, add it to the array 
• Number is changed to 10. 10/2, remainder is 0, quotient is 5, add it to the array [1,0]
• Number is changed to 5. 5/2, remainder is 1, quotient is 2, add it to the array [1,0,1]
• Number is changed to 2. 2/2, remainder is 0, quotient is 1, add it to the array [1,0,1,0]
• Number is changed to 1. 1/2, remainder is 1, quotient is 0, add it to the array [1,0,1,0,1]

Now, the reverse of the array content is 10101, which is the binary value of 21.

### Method 1: C# program with an array:

The below C# program uses the above algorithm to convert a decimal value to binary.

``````using System;
using System.Text;

namespace Program
{
class Program
{
static String getBinaryString(int d)
{
int[] arr = new int;
int i = 0;
StringBuilder binaryBuilder = new StringBuilder();

while (d > 0)
{
arr[i] = d % 2;
d = d / 2;
i++;
}

i--;
while (i >= 0)
{
binaryBuilder.Append(arr[i]);
i--;
}
return binaryBuilder.ToString();
}

static void Main(string[] args)
{
int no;
Console.WriteLine("Enter the decimal number:");

Console.WriteLine("Binary: " + getBinaryString(no));
}
}
}``````

Here,

• getBinaryString method is used to convert a decimal value to binary. It takes the decimal value as the parameter and returns the binary value of that number as string.

• It creates an array arr to hold the binary value. binaryBuilder is a StringBuilder object to create the final string.
• The first while loop finds the remainders and appends them to the array.
• The second while loop iterates through this array in reverse order and appends these values to the StringBuilder.
• It returns the string value of the StringBuilder object.

If you run this program, it will give output as like below:

``````Enter the decimal number:
21
Binary: 10101

Enter the decimal number:
19
Binary: 10011``````

### Method 2: Without using an array to hold the values:

We can also remove the array to hold the remainder values. Without using the array, we can add the remainder to the start of the StringBuilder. The StringBuilder class provides a method called Insert that takes the index position as the first parameter and adds a string to a specific index.

In the below program, we are using only a StringBuilder to create the final binary string:

``````using System;
using System.Text;

namespace Program
{
class Program
{
static String getBinaryString(int d)
{
int i = 0, remainder;
StringBuilder binaryBuilder = new StringBuilder();

while (d > 0)
{
remainder = d % 2;
binaryBuilder.Insert(0, remainder.ToString());
d = d / 2;
i++;
}

return binaryBuilder.ToString();
}

static void Main(string[] args)
{
int no;
Console.WriteLine("Enter the decimal number:");

Console.WriteLine("Binary: " + getBinaryString(no));
}
}
}``````

One more advantage of this approach is that we removed the second while loop. It will be faster than the previous one.

It will give similar output.

### Method 3: Recursive way:

This is another way to convert a decimal value to binary in C#. A recursive method calls itself again and again to find the final result. We can write a recursive method to get the binary value for a decimal number.

``````using System;
using System.Text;

namespace Program
{
class Program
{
static String getBinaryString(int d)
{
if (d < 2) return d.ToString();

int divisor = d / 2;
int remainder = d % 2;

return getBinaryString(divisor) + remainder;
}

static void Main(string[] args)
{
int no;
Console.WriteLine("Enter the decimal number:");

Console.WriteLine("Binary: " + getBinaryString(no));
}
}
}``````

getBinaryString is changed to a recursive method. It will give similar result.

### Method 4: By using Convert.ToString:

Convert.ToString method is the easiest way to covert a decimal value to any other base. This method is defined as like below:

``````public static string ToString (short value, int toBase);

public static string ToString (long value, int toBase);``````

We can convert a 32 bit or 64 bit integer to another base string value. The base can be 2, 8, 10 or 16. For binary, we must provide 2.

``````using System;

namespace Program
{
class Program
{
static void Main(string[] args)
{
int no;
Console.WriteLine("Enter the decimal number:");
``````Enter the decimal number: