Number Validation Program

Its OK, especially when you are just getting started, to practice learning C# on some smaller code projects.  Don’t underestimate how much you can learn about programming in the console before you move on to more complex tasks.

For this program, we are going to validate a number that the user gives us in a console application. Here are the specifications:

Write a program in console that accepts an Integer value from the user

When you have accepted the value, determine if it is less than or equal to 10.  If so, print the statement: “This value is small”.

If its larger than 10, print the statement: “This value is large”.

If the user enters a number greater than 100, print the statement: “This value is too big!” but don’t print either of the previous statements

Hint: Remember to use Convert.ToInt32 when accepting the integer value from the Console.ReadLine() statement so you can store the incoming value into an integer variable.

Try it out on your own and see if you can get it working. If you get stuck, check the answer below.

[codesyntax lang=”csharp” lines_start=”1″ title=”Solution” blockstate=”collapsed”]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NumberValidation
{
    class NumberValidationProgram
    {
        static void Main(string[] args)
        {
            //print a welcome statement
            Console.WriteLine("Welcome to the number validation program");
            Console.WriteLine("Please enter a number");

            //accept the number from the console
            int number = Convert.ToInt32(Console.ReadLine());

            //check the number against the validation rules
            if(number <= 10)
            {
                Console.WriteLine("This value is small");
            }
            else if(number > 10 && number <= 100)
            {
                Console.WriteLine("This value is large");
            }
            else if(number > 100)
            {
                Console.WriteLine("This value is too big!");
            }
        }
    }
}

[/codesyntax]

Leave a Reply

Your email address will not be published. Required fields are marked *