Write a program which requests a number between 1 and 50 from the user. It must repeatedly ask
the user for a number until a valid number is entered.
After receiving a valid number, the program must display all the even numbers smaller than that
number. (If the user enters 11, the numbers 2,4,6,8,10 must be displayed)
using System;
namespace Test
{
class InputTest
{
static int Main()
{
int number;
while(true)
{
Console.Write("Enter number a number between 1 and 50: ");
string line = Console.ReadLine();
if(!int.TryParse(line, out number))
{
Console.WriteLine("Bad input");
return 1;
}
if(1 <= number && number <= 50)
{
break;
}
Console.WriteLine("Error: the number does not fall within the specified range\n");
}
for(int i = 2; i < number; i+=2)
{
Console.Write(i + " ");
}
Console.WriteLine();
return 0;
}
}
}
Comments
Leave a comment