-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
41 lines (37 loc) · 822 Bytes
/
Copy pathProgram.cs
File metadata and controls
41 lines (37 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
while (true)
{
Console.Write("Enter up to 8 binary digits, enter [e]xit to close the program: ");
string input = Console.ReadLine()!;
if (input == "e" || input == "exit")
{
break;
}
if (input.Length <= 8 && input.Length > 0)
{
int convert = ConvertBin2Dec(input);
Console.WriteLine($"Decimal: {convert} ");
}
else
{
Console.WriteLine("Not a binary number");
}
}
int ConvertBin2Dec(string input)
{
int convert = 0;
foreach (char digit in input)
{
if (digit == '0' || digit == '1')
{
convert <<= 1;
convert += digit - '0';
}
else
{
Console.WriteLine($"{digit} is not 0 or 1");
convert = 0;
}
}
return convert;
}