Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to take input as "Integer" value only between 20 to 50. It should give error if any string value is taken or any value which is out side the range of 20 to 50.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace Console_Test
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Enter the integer value only");
            int number = Convert.ToInt32(Console.ReadLine());

            if (number <= 20 && number >= 50)
            {
                Console.WriteLine("Please enter the number between 20 to 50");
            }

           // Program should not close and it should display again from start unless user enter the value between 20 to 50.


          
        }

        
    }
}
Posted
Updated 16-Dec-15 4:26am
v3

Use int.TryParse[^] to attempt to convert the user's input to a number.

Use the OR operator (||) to test whether the number is outside of the range. It can't be both "less than or equal to 20" and "greater than or equal to 50" at the same time!

Add a loop to re-prompt the user until you get a valid number.

C#
int number;
bool isValid = false;
while (!isValid)
{
    Console.WriteLine("Enter the integer value only");
    string input = Console.ReadLine();
    
    if (!int.TryParse(input, out number))
    {
        Console.WriteLine("The string '{0}' is not an integer.", input);
    }
    else if (number <= 20 || number >= 50)
    {
        Console.WriteLine("Please enter the number between 20 to 50");
    }
    else
    {
        isValid = true;
    }
}

Console.WriteLine("You entered {0}.", number);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Dec-15 11:07am    
Sure, a 5.
—SA
Aditakumar2311 16-Dec-15 16:00pm    
Sir thank you for helping me.

The output of program like this.


Message- Enter an integer value between 20 and 50, exclusive

User enters - JKljdlkfjdfklj

You have entered the wrong values, please enter an integer value between 20 and 50, exclusive

User enters - 60

You have entered the wrong values, please enter an integer value between 20 and 50, exclusive

User enters - 25

Message- You have entered the correct integer value.
A do..while loop could do it:
C#
int number;
bool ok;
do {
   Console.WriteLine("Enter an integer value between 20 and 50, exclusive");
   ok = Int.TryParse(Console.ReadLine(), out number);
   if (!ok) {
      number = 0;
   }
} while ((number <= 20) || (number >= 50))

This will keep asking for a valid value until in right range.

Hope this helps.
 
Share this answer
 
Comments
Richard Deeming 16-Dec-15 10:43am    
Snap! :)
phil.o 16-Dec-15 10:47am    
:)
Sergey Alexandrovich Kryukov 16-Dec-15 11:07am    
Sure, a 5.
—SA
phil.o 16-Dec-15 11:25am    
Thank you Sergey :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900