Enter the Number : 3 Binary Format for the Given Number is : 11
Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
C# Program to Convert Decimal to Binary
C# Program to Convert Decimal to Binary
This is a C# Program to convert decimal to binary.
This C# Program Converts Decimal to Binary.
Here the number in decimal form is obtained and is it repeatedly divided by 2 and its binary form is obtained.
Here is source code of the C# Program to Convert Decimal to Binary. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to Convert Decimal to Binary */ using System; class myclass { static void Main() { int num; Console.Write("Enter a Number : "); num = int.Parse(Console.ReadLine()); int quot; string rem = ""; while (num >= 1) { quot = num / 2; rem += (num % 2).ToString(); num = quot; } string bin = ""; for (int i = rem.Length - 1; i >= 0; i--) { bin = bin + rem[i]; } Console.WriteLine("The Binary format for given number is {0}", bin); Console.Read(); } }
This C# program, we are declaring the variable ‘num’ and ‘bin’ Integer type. Declare the variable out type string which will assign the value ‘nothing’. Using while loop check the value of ‘num’ variable is greater than 0 and also greater than 1.
Convert the value of ‘bin’ variable into String type to achieve the concatenation. The variable “out” is going to get the bin’s value (which contain the division residue) concatenated with the same variable “out” to keep accumulating the values of the others residues that generates while the “WHILE” runs. Finally assign to the textbox txt_bin, the value of the variable “out”.