Conversion of Decimal to Dinary number

This is the code writen in c sharp .by which we can simply convert a decimal number in to binary


using System;

class Program{

static void Main(string[] args){

try{

int i = (int)Convert.ToInt32(args[0]);
Console.WriteLine("\n{0} converted to Binary is {1}\n",i,ToBin(i));

}catch(Exception e){

Console.WriteLine("\n{0}\n",e.Message);

}

}//here the insilize the system


public static string ToBinary(Int32 Decimal)
{
//declare flag to need
Int32 Binflag;
char[] BinArray;
string Bin = "";

while (Decimal > 0)
{
Binflag = Decimal % 2;
Bin += Binflag ;
Decimal = Decimal / 2;
}

BinArray = Bin.ToCharArray();
Array.Reverse(BinArray);
Bin = new string(BinArray);

return Bin;
}


}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: