w3resource

C#: Check whether three given integer values are in the range 20..50 inclusive


Check Integers in Range 20-50

Write a C# Sharp program to check whether three given integer values are in the range 20..50 inclusive. Return true if 1 or more of them are in the said range otherwise false.

Visual Presentation:

C# Sharp: Basic Algorithm Exercises - Check whether three given integer values are in the range 20..50 inclusive.

Sample Solution:

C# Sharp Code:

using System;

// Namespace declaration
namespace exercises
{
    // Class declaration
    class Program
    {
        // Main method - entry point of the program
        static void Main(string[] args)
        {
            // Displaying the results of the 'test' method with different integer trios
            Console.WriteLine(test(11, 20, 12)); // Output: True
            Console.WriteLine(test(30, 30, 17)); // Output: True
            Console.WriteLine(test(25, 35, 50)); // Output: True
            Console.WriteLine(test(15, 12, 8));  // Output: False
            Console.ReadLine();                  // Keeping the console window open
        }

        // Method to test if at least one number in a trio is within the range 20 to 50 (inclusive)
        public static bool test(int x, int y, int z)
        {
            // Check if x is in the range [20, 50] OR y is in the range [20, 50] OR z is in the range [20, 50]
            // Return true if at least one of x, y, or z (or more) is within the specified range; otherwise, return false
            return (x >= 20 && x <= 50) || (y >= 20 && y <= 50) || (z >= 20 && z <= 50);
        }
    }
}


Sample Output:

True
True
True
False

Flowchart:

C# Sharp: Flowchart: Check whether three given integer values are in the range 20..50 inclusive.

For more Practice: Solve these Related Problems:

  • Write a C# program to verify if at least two out of three integers fall in the range 20–50 inclusive.
  • Write a C# program to check whether all three given integers lie in different halves of the 20–50 range (i.e., 20–34 and 35–50).
  • Write a C# program to find the median of three integers if at least one lies in the 20–50 range.
  • Write a C# program that returns true if one value is in the 20–50 range and the other two are outside.

Go to:


PREV : One Integer in Range 100-200.
NEXT : One of Two Integers in Range 20-50.

C# Sharp Code Editor:



Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.