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).
internal class Program
{
static void Main(string[] args)
{
do
{
Console.WriteLine("Enter a number between 1 and 50");
int number = int.Parse(Console.ReadLine());
if (number < 50 && number > 1)
{
DisplayingEvenNumbers(number);
break;
}
else
Console.WriteLine("Entered an invalid number");
}
while (true);
Console.ReadKey();
}
public static void DisplayingEvenNumbers(int number)
{
Console.WriteLine($"You entered a number: {number}");
Console.Write("Even Numbers:");
for (int i = 1; i < number; i++)
{
if (i % 2 == 0)
Console.Write($" {i} ");
}
}
}
Comments
Leave a comment