w3resource

C#: Check a given array of integers and return true if the array contains 10 or 20 twice


C# Sharp Basic Algorithm: Exercise-100 with Solution

Check if 10 or 20 Appears Twice in Array

Write a C# Sharp program to check a given array of integers and return true if it contains 10 or 20 twice. The array length will be 0, 1, or 2.

Visual Presentation:

C# Sharp: Basic Algorithm Exercises - Check a given array of integers and return true if the array conains 10 or 20 twice.

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[] { 20, 20 })); // Calling test method with array [20, 20] and displaying the result
            Console.WriteLine(test(new[] { 10, 10 })); // Calling test method with array [10, 10] and displaying the result
            Console.WriteLine(test(new[] { 10 })); // Calling test method with array [10] and displaying the result
        }

        // Method to check if the input array has a length of 2 and contains either two 10s or two 20s
        public static bool test(int[] nums)
        {
            // Checking if the array length is equal to 2
            // and if the elements are either both 10 or both 20
            return nums.Length == 2
&& ((nums[0] == 10 && nums[1] == 10)
                     || (nums[0] == 20 && nums[1] == 20));
        } 
    }
}

Sample Output:

False
True
True
False

Flowchart:

C# Sharp: Flowchart: Check a given array of integers and return true if the array conains 10 or 20 twice.

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 of integers and length 1 or more. The length of the new array will be double length of the given array and all the elements are 1 except the first  element which is equal to the given array.
Next: Write a C# Sharp program to check a given array of integers, length 3 and create a new array. If there is a 5 in the given array immediately followed by a 7 then set 7 to 1.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/csharp-exercises/basic-algo/csharp-basic-algorithm-exercises-100.php