Saturday, November 15, 2014

Conversion of Decimal to Binary in C#

In interviews, there are some programming questions which you will find most of the places like Write a program to reverse the string or Write a program to check string are palindrome or not etc..

In this post, I have a picked a question from that type of programming questions i.e.

Write a program for converting the decimal value to binary value?

Below is the simple method for converting a decimal value into binary value  :

Code Snippet


/// <summary>
/// Method for converting Decimal to Binary
/// </summary>
/// <param name="_value">integer value</param>
/// <returns></returns>
static string DecimalToBinary(int _value)
{
  string strBinary = string.Empty;
  while (_value >= 1)
   {
     strBinary = (_value % 2).ToString() + strBinary;
     _value = _value / 2;
   }
   return strBinary;
}

No comments:

Post a Comment

If You Enjoyed This Post Please Take 5 Seconds To Share It.

^ Scroll to Top hgpromo