Console.Read
reads the next character from the standard input stream, and returns its code point. This is not the same as its integer value. For example, the code point of the character '7'
is 55
. Additionally, Read
only considers a single character; thus, when you type 13
, it will only return the code point for '1'
.
Instead of Read
, you should use ReadLine
, which will read an entire line of characters from standard input (i.e. until the user presses "Enter") and return them as a string. Then, you can convert this string into a long value by using its Parse
method.
long u = long.Parse(Console.ReadLine());
If you want to avoid getting exceptions when the user enters an incorrect input (such as letters instead of digits), you should use TryParse
:
string s = Console.ReadLine();
long u;
if (long.TryParse(s, out u))
primefinder(u);
else
Console.WriteLine("Your input was not a valid number!");