Binary Number: A Number expressed using the base 2 with digits 0 and 1.
Decimal Number: A Number expressed using the base 10 with digits 0 to 9.
Conversion Example: Suppose we want to convert binary number 1011 to decimal. The followiing is the conversion process:
Step 1: 1* 2^0=1
Step 2: 1* 2^1=2
Step 3: 0*2^2=0
Step 4: 1*2^3=8
Its decimal value = 1 + 2 + 0 + 8 = 11
So (1011)2 = (11)10 class BinaryToDecimal {
public int Power(int n) {
if (n == 0) return 1;
else return 2 * Power(n - 1);
}
static void Main() {
BinaryToDecimal bd = new BinaryToDecimal();
Console.WriteLine("Enter any binary no");
string s1 = Console.ReadLine();
string s2 = "";
for (int i = s1.Length - 1; i >= 0; i--) {
s2 += s1[i];
}
int sum = 0;
for (int i = 0; i < s2.Length; i++) {
int res = bd.Power(i);
sum = sum + (res * int.Parse(s2[i].ToString()));
}
Console.WriteLine(sum);
Console.ReadLine();
}
}