Author : Rahul

Write a program to convert binary number to decimal number


Program

using System;

namespace ProgrammingQuestion
{
 class Program
 {
   static void Main(string[] args)
   {
     //Variable Declaration
     string binaryNumberVal;
     int binaryNumber, decimalNumber = 0, tempNumber, digit, power = 0;

     //User Input
     Console.Write("Enter Binary Number:");
     binaryNumberVal = Console.ReadLine();
     binaryNumber = Convert.ToInt32(binaryNumberVal);
     
     //Binary to Decimal Logic
     tempNumber = binaryNumber;
     while (tempNumber > 0)
     {
       digit = tempNumber % 10;
       decimalNumber = decimalNumber + digit * (int)Math.Pow(2, power);
       power++;
       tempNumber = tempNumber / 10;
     }
     
     //Print Result
     Console.WriteLine($"Decimal Number: {decimalNumber}.");
     
     Console.Read();
   }
 }
}

Output

Enter Binary Number:11001
Decimal Number: 25