w3resource

C#: Check whether a given array of integers of length 2, contains 15 or 20


C# Sharp Basic Algorithm: Exercise-97 with Solution

Write a C# Sharp program to check if an array of integers length 2 contains 15 or 20.

Visual Presentation:

C# Sharp: Basic Algorithm Exercises - Check whether a given array of integers of length 2, contains 15 or 20.

Sample Solution:-

C# Sharp Code:

using System; // Importing the System namespace

namespace exercises // Defining a namespace called 'exercises'
{
    class Program // Defining a class named 'Program'
    {
        static void Main(string[] args) // The entry point of the program
        {         
            // Output the result of the 'test' method with different integer arrays as arguments
            Console.WriteLine(test(new[] { 12, 20 })); // Calling test method with array [12, 20] and displaying the result
            Console.WriteLine(test(new[] { 14, 15 })); // Calling test method with array [14, 15] and displaying the result
            Console.WriteLine(test(new[] { 11, 21 })); // Calling test method with array [11, 21] and displaying the result
        }        

        // Method to check if any element in the input array is equal to 15 or 20
        public static bool test(int[] nums)
        {
            // Returning true if any element in the array is equal to 15 or 20
            return nums[0] == 15 || nums[0] == 20 || nums[1] == 15 || nums[1] == 20;
        } 
    }
}

Sample Output:

True
True
False

Flowchart:

C# Sharp: Flowchart: Check whether a given array of integers of length 2, contains 15 or 20.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to create a new array taking the first and last elements of a given array of integers of length 1 or more.
Next: Write a C# Sharp program to check whether a given array of integers of length 2, does not contain 15 or 20.

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.