Answer to Question #326492 in C# for Sando

Question #326492

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.

1
Expert's answer
2022-04-10T05:24:31-0400
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 + " ");
        }
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS