Latest Post

Tuesday, 3 January 2017

How to convert decimal number to binary number [C#]

Write a program in C# to convert decimal number into binary number.

Following is the simple C# code :
class Lab
    {
        static void Main(string[] args)
        {
            int number, decimalValue = 0, reminderValue;
            Console.Write("\nEnter a Number that you want binary : ");
            number = Convert.ToInt32(Console.ReadLine());
            decimalValue = number;

            string binaryNumber = string.Empty;
            while (number > 0)
            {
                reminderValue = number % 2;
                number /= 2;
                binaryNumber = reminderValue.ToString() + binaryNumber;
            }
            Console.Write("\nEntered a Number = " + decimalValue);
            Console.Write("\nThe Binary format for given number is =" + binaryNumber);

            Console.ReadLine();
        }
    }

Output:-
Enter a Number that you want binary : 78

Entered a Number = 78
The Binary format for given number is =1001110


Government Jobs