Advertisement

Latest Post

Tuesday, 3 January 2017

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

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

Following is the simple C# code :
class Lab
    {
        static void Main(string[] args)
        {

            Console.Write("Enter a Binary Number(1s and 0s) : ");
            int number, Entered_binaryVal, decimalValue = 0, baseValue = 1, reminderValue;           
            number = int.Parse(Console.ReadLine());
            Entered_binaryVal = number;
            while (number > 0)
            {
                reminderValue = number % 10;
                decimalValue = decimalValue + reminderValue * baseValue;
                number = number / 10;
                baseValue = baseValue * 2;
            }
            Console.Write("The Binary Number is : " + Entered_binaryVal);
            Console.Write("\nThe Decimal value is : " + decimalValue);
            Console.ReadLine();

            Console.ReadLine();
        }
    }

Output:-
Enter a Binary Number(1s and 0s) : 10101
The Binary Number is : 10101
Its Decimal value is : 21


Government Jobs