C# Program to Convert a Decimal Number Into a Binary Number

I’m writing every article I think will help you in some way. in that respect C# Program to Convert a Decimal Number Into a Binary Number This article will be very useful for you.

C# Program to Convert a Decimal Number Into a Binary Number

C# Program to Convert a Decimal Number Into a Binary Number

//C# program to convert a decimal number to the binary number

using System;

class Program
{
    static void Main(string[] args)
    {
        int decNum  =   0;
        int binNum  =   0;
        
        string tempRem = "";
        
        Console.Write("Enter a decimal number : ");
        decNum = int.Parse(Console.ReadLine());

        while (decNum >= 1)
        {
            tempRem += (decNum % 2).ToString();
            decNum = decNum / 2;
        }

        for (int i = tempRem.Length - 1; i >= 0; i--)
        {
            binNum = binNum*10 + tempRem[i]-0x30;
        }

        Console.WriteLine("Binary Number: "+binNum);
    }
}
Enter a decimal number : 9
Binary Number: 1001
Press any key to continue . . .

Final Words

C# Program to Convert a Decimal Number Into a Binary Number Thank you for reading this useful article. Also, let us know if you have any doubts about this article. We are committed to helping you.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment