Compare number within a numeric range

3

I want to create a function that receives a number and check that it is within a range of 1 to 25. What is the best way to do this?

private int VerificaIntervalo(int numero)
{
    if (numero > 25)
    {
        return 0;
    }
    return 1;
}
    
asked by anonymous 19.01.2016 / 18:50

4 answers

5

It's much simpler than other options:

private bool VerificaIntervalo(int numero) {
   return numero >= 1 && numero <= 25;
}

Ideally the method name could be more explanatory than this.

    
19.01.2016 / 19:05
2

The simplest would be:

private bool VerificaIntervalo(int numero)
{
   if(numero >= 1 && numero <= 25)
     return true;
   else
     return false;
}
    
19.01.2016 / 18:54
1

Here's an example for you to check if the numbers are in the range of 1 to 25, see below:

using System;

public class Test
{
    public static void Main()
    {
        if (comparaNumero(90))
           Console.WriteLine("Dentro do intervalo entre 1 a 25");
        else
           Console.WriteLine("Fora do intervalo");
    }

    public static bool comparaNumero(int n)
    {
        return (n >= 1 && n <= 25);
    }
}

See worked on IdeOne .

    
19.01.2016 / 19:08
0
private bool VerificaIntervalo(int numero)
    {
          if(numero >= 1 && numero <= 25)
          {
            return true;
          }
          else
          {
           return false;
          }
    }

Would that be?

    
19.01.2016 / 19:02