Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Basic Algorithm Exercises: Check a given array of integers and return true if the array contains three increasing adjacent numbers


C# Sharp Basic Algorithm: Exercise-126 with Solution

Write a C# Sharp program to check a given array of integers and return true if the array contains three increasing adjacent numbers.

Pictorial Presentation:

C# Sharp: Basic Algorithm Exercises - Check a given array of integers and return true if the array contains three increasing adjacent numbers.

Sample Solution:

C# Sharp Code:

using System;
namespace exercises
{
   class Program
    {       
        static void Main(string[] args)
        {               
          Console.WriteLine(test(new[] { 1, 2, 3, 5, 3, 7 })); 
          Console.WriteLine(test(new[] { 3, 7, 5, 5, 3, 7 })); 
           Console.WriteLine(test(new[] { 3, 7, 5, 5, 6, 7, 5 }));   
        }               
       static bool test(int[] numbers)
         {
           for (int i = 0; i <= numbers.Length - 3; i++)
            {
                if (numbers[i] == numbers[i + 1] - 1
                    && numbers[i] == numbers[i + 2] - 2)
                {
                    return true;
                }
            }
            return false;
         }    
   } 
}

Sample Output:

True
False
True

Flowchart:

C# Sharp: Flowchart: Check a given array of integers and return true if the array contains three increasing adjacent numbers.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to check a given array of integers and return true if the specified number of same elements appears at the start and end of the given array.
Next: Write a C# Sharp program to shift an element in left direction and return a new array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.