← C# from Scratch Lesson 38 of 39 · 5 XP

Warm-up: is it a number? warm-up

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

int.Parse fails with an error if the line is not a number. A calm way is int.TryParse: it returns true or false and puts the number into the variable after out:

string s = Console.ReadLine();
if (int.TryParse(s, out int n))
{
    Console.WriteLine($"ok {n}");
}
else
{
    Console.WriteLine("not a number");
}

Task. Read a line. If it is an integer, print ok and the number separated by a space, otherwise not a number.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.