w3resource
C# Sharp Programming Exercises

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?



New Content: Composer: Dependency manager for PHP, R Programming