Code to Convert Decimal to Binary

Manojjupally
Posted by Manojjupally under C# category on | Points: 40 | Views : 1864
Convert Decimal to Binary


public static void ConvertDecimalToBinary()
{
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;
}

// Reversing the value
string bin = "";
for (int j = rem.Length - 1; j >= 0; i--)
{
bin = bin + rem[j];
}

Console.WriteLine("The Binary format for given number is {0}", bin);
}

Comments or Responses

Login to post response