C# Program to Convert Decimal to Binary

This is a C# Program to convert decimal to binary.

Problem Description

This C# Program Converts Decimal to Binary.

Problem Solution

Here the number in decimal form is obtained and is it repeatedly divided by 2 and its binary form is obtained.

Program/Source Code

Here is source code of the C# Program to Convert Decimal to Binary. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Convert Decimal to Binary
 */
using System;
class myclass
{
    static void Main()
    {
        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;
        }
        string bin = "";
        for (int i = rem.Length - 1; i >= 0; i--)
        {
            bin = bin + rem[i];
        }
        Console.WriteLine("The Binary format for given number is {0}", bin);
        Console.Read();
    }
}
Program Explanation

This C# program, we are declaring the variable ‘num’ and ‘bin’ Integer type. Declare the variable out type string which will assign the value ‘nothing’. Using while loop check the value of ‘num’ variable is greater than 0 and also greater than 1.

advertisement

The variable bin will get the value of the operation, the variable num value % 2; the Mod is used to obtain a division residue. Divide the numbers to indicate next to the symbol %, this case the number 2, because 2 is the binary numbers base. Then after obtaining the first residue the variable ‘num’ will get its own value between 2.

Convert the value of ‘bin’ variable into String type to achieve the concatenation. The variable “out” is going to get the bin’s value (which contain the division residue) concatenated with the same variable “out” to keep accumulating the values of the others residues that generates while the “WHILE” runs. Finally assign to the textbox txt_bin, the value of the variable “out”.

Runtime Test Cases
 
Enter the Number : 3
Binary Format for the Given Number is : 11

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Free 30-Day Java Certification Bootcamp is Live. Join Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.