Answer to Question #320398 in C# for kun

Question #320398

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).

1
Expert's answer
2022-03-29T16:48:31-0400
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} ");
            }
        }
    }

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