Answer to Question #190317 in C# for Nkhululeko chabala

Question #190317

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
2021-05-07T11:41:56-0400
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;
        }
    }
}

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
APPROVED BY CLIENTS