;

C# Program to Convert Decimal Number to Binary Number


Tutorialsrack 11/08/2019 C# Programs

In this C# program, we will learn how to write a program to convert decimal number to binary number.

What is the Decimal Number?

In the context of Computer, Decimal is a term that describes the base-10 number system, probably the most commonly used number system.  Digits from 0 to 9 are also known as Denary. The decimal number system consists of ten single-digit numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. 

What is the Binary Number?

A binary number is a number expressed in the base-2 numeral system or binary numeral system, which uses only two symbols: typically "0" (zero) and "1" (one).  The base-2 numeral system is a positional notation with a radix of 2. Each digit is referred to as a bit. Because of its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used by almost all modern computers and computer-based devices.

For Example, 1011 is a binary number which is equivalent to 11

Here is the code of the program to convert decimal number to binary number:

Code - C# Program To Convert Decimal Number to Binary Number
using System;

namespace TutorialsrackPrograms
{
    class Program
    {
        //C# Program to Convert Decimal Number to Binary Number.
        static void Main(string[] args)
        {
            string Number, ConvertedToBinary = string.Empty;

            Console.Write("Enter The Number: ");
            Number = Console.ReadLine();
            int num = Convert.ToInt32(Number);
            while (num > 1)
            {
                int remainder = num % 2;
                ConvertedToBinary = Convert.ToString(remainder) + ConvertedToBinary;
                num /= 2;
            }
            ConvertedToBinary = Convert.ToString(num) + ConvertedToBinary;
            Console.WriteLine("Decimal Number Converted to Binary Number: {0}", ConvertedToBinary);
            Console.Read();
        }
    }
}
Output

Enter The Number: 11
Decimal Number Converted to Binary Number: 1011

Enter The Number: 65
Decimal Number Converted to Binary Number: 1000001

Another Way To Convert Decimal Number To Binary Number

Code - C# Program To Convert Decimal Number to Binary Number
using System;

namespace TutorialsrackPrograms
{
    class Program
    {
        //C# Program to Convert Decimal Number to Binary Number.
        static void Main(string[] args)
        {
            int Number, ConvertedToBinary;
            Console.Write("Enter The Number: ");
            Number = int.Parse(Console.ReadLine());
            ConvertedToBinary = int.Parse(Convert.ToString(Number, 2)); //conversion occurs here 
            Console.WriteLine("Decimal Number Converted to Binary Number: {0}", ConvertedToBinary);
            Console.Read();
        }
    }
}
Output

Enter The Number: 11
Decimal Number Converted to Binary Number: 1011

Enter The Number: 65
Decimal Number Converted to Binary Number: 1000001


Related Posts



Comments

Recent Posts
Tags