I am executing the following code, and without introducing a breakpoint I cannot execute the last two lines ., It doesn't execute NumberOut
Console.WriteLine("Enter a positive whole number: ");
int NumberIn = Console.Read();
int NumberOut = 0;
int count = 0;
while (NumberIn > 0)
{
count = count + 1;
int PartValue = NumberIn % 2;
NumberIn = NumberIn / 2;
for (int i = 0; i < count - 1; i++)
{
PartValue = PartValue * 10;
}
NumberOut = NumberOut + PartValue;
}
Console.WriteLine("the result is: {0}", NumberOut);
Console.ReadLine();
Console.Read()
, this will not read an integer and return that, it will take the first character the user typed, convert that to its encoding value, and return that. For instance, if you type 1000, it will return 49 (49 = the code point for the digit '1'). Could it be that you typed in, say, 4, and expected the sequence 4, 2, 1, 0 and got 52, 26, 13, 6, 3, 1, 0 instead? That it just loops a few more times than expected?