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.
using System;
class Program
{
static void Main(String[] args)
{
int number = 0;
try
{
do
{
Console.Write("Enter number between 1 and 50: ");
number = Int32.Parse(Console.ReadLine());
} while (number < 1 || number > 50);
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message} Try again!");
}
for(int i = 1; i < number; i++)
{
if(i % 2 == 0)
Console.Write(i + " ");
}
}
}
Comments
Leave a comment