w3resource

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


C# Sharp Basic Algorithm: Exercise-16 with Solution

Write a C# Sharp program to check whether two given integer values are in the range 20..50 inclusive. Return true if 1 or other is in the range, otherwise false.

Visual Presentation:

C# Sharp: Basic Algorithm Exercises - Check whether two 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 pairs of integers
            Console.WriteLine(test(20, 84)); // Output: True
            Console.WriteLine(test(14, 50)); // Output: True
            Console.WriteLine(test(11, 45)); // Output: True
            Console.WriteLine(test(25, 40)); // Output: False
            Console.ReadLine();             // Keeping the console window open
        }

        // Method to test conditions for two integers x and y
        public static bool test(int x, int y)
        {
            // Check if x is less than or equal to 20 OR y is greater than or equal to 50
            // OR Check if y is less than or equal to 20 OR x is greater than or equal to 50
            // If any of these conditions is true, return true; otherwise, return false
            return (x <= 20 || y >= 50) || (y <= 20 || x >= 50);
        }
    }
}

Sample Output:

True
True
True
False

Flowchart:

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

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: 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.
Next: Write a C# Sharp program to check if a string 'yt' appears at index 1 in a given string. If it appears return a string without 'yt' otherwise return the original string.

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.