Back to: C#.NET Programs and Algorithms

**Decimal to Binary Conversion in C# with Examples**

In this article, I am going to discuss the **Decimal to Binary Conversion in C#** with Examples. Please read our previous article where we discussed the **Sum of Digits of a given number Program in C#** in many different ways. In C#, we can easily convert any decimal number (base-10 (i.e. 0 to 9)) into binary number (base-2 (i.e. 0 or 1)). As part of this article, we are going to discuss the following pointers.

**What are Decimal Numbers?****What are Binary Numbers?****How to Convert Decimal to Binary in C#?**

**What are Decimal Numbers?**

The Decimal numbers are the numbers whose base is 10. That means the decimal numbers are range from 0 to 9. Any combination of such digits (digits from 0 to 9) is a decimal number such as 2238, 1585, 227, 0, 71, etc.

**What are Binary Numbers?**

The Binary numbers are the numbers whose base is 2. That means the binary numbers can be represent using only two digits i.e. 0 and 1. So, any combination of these two numbers (i.e. 0 and 1) is a binary number such as 101, 10o1, 111111, 10101, etc. Let us have a look at the following image which shows the decimal number along with its binary representation.

**Algorithm: Decimal to Binary Conversion **

**Step1:** First, divide the number by 2 through the modulus (%) operator and store the remainder in an array

**Step2:** Divide the number by 2 through the division (/) operator.

**Step3:** Repeat step 2 until the number is greater than zero.

**Program: Decimal to Binary Conversion in C#**

using System; namespace LogicalPrograms { public class Program { static void Main(string[] args) { Console.Write("Enter the Decimal Number : "); int number = int.Parse(Console.ReadLine()); int i; int[] numberArray = new int[10]; for (i = 0; number > 0; i++) { numberArray[i] = number % 2; number = number / 2; } Console.Write("Binary Represenation of the given Number : "); for (i = i - 1; i >= 0; i--) { Console.Write(numberArray[i]); } Console.ReadKey(); } } }

**Output:**

**Let us see another way of doing the conversion in C#.**

In the following program, we created a string variable to hold the binary representation of the Decimal number.

using System; namespace LogicalPrograms { public class Program { static void Main(string[] args) { Console.Write("Enter the Decimal Number : "); int number = int.Parse(Console.ReadLine()); string Result = string.Empty; for (int i = 0; number > 0; i++) { Result = number % 2 + Result; number = number / 2; } Console.WriteLine($"Binary Represenation of the given Number : {Result}"); Console.ReadKey(); } } }

**Output:**

In the next article, I am going to discuss the** Binary to Decimal Conversion Program in C#** with Examples. Here, in this article, I try to explain the **Decimal to Binary Conversion in C# **with Examples.