C#: Program to convert any decimal number to binary

In this C# tutorial we are "converting any decimal number entered by the user to binary" . Here we are taking four variables "num" for number ,"i" for counter ,"rem" for remainder and "z" for output respectively. Now, lets get started!!

using System;
​class conv
{
 public static void Main(String []args)
 {
        int num,i=1,rem,z=0;
        Console.WriteLine("Please enter any decimal number : ");
        num=Convert.ToInt32(Console.ReadLine());
        while(num!=0)
        { 
          rem=num%2;
          num=num/2;
          z=z+rem*i;
          i=i*10;
        }
      Console.WriteLine(z);
}
}
SHARE

Raunak Hajela

Hi. I’m CEO/Founder of Kodestat. I’m Student, Geek, Web Designer, Developer, Blog Enthusiast and Player. Inspired to make things looks better. I like playing with codes plus I am marvel fan :-P You can check my design portfolio here. For consulation fell free to drop an email at raunakhajela@gmail.com.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment