w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Find the pair of adjacent elements that has the highest product of an given array of integers

C# Sharp Basic: Exercise-57 with Solution

Write a C# program to find the pair of adjacent elements that has the highest product of an given array of integers.

Sample Solution:

C# Sharp Code:

using System;
public class Example
{
   
    public static int adjacent_Elements_Product(int[] input_Array)
        {
        int max = 0;
        for (int i=1; i<input_Array.Length; ) {
        max = Math.Max(max, input_Array[i-1] * input_Array[i++]);
        }
          return max;
      }
        
    public static void Main()
        {
             Console.WriteLine(adjacent_Elements_Product(new int[] {1 , 3, 4, 5, 2}));
             Console.WriteLine(adjacent_Elements_Product(new int[] {1 , 3, -4, 5, 2}));
             Console.WriteLine(adjacent_Elements_Product(new int[] {1 , 0, -4, 0, 2}));
         }
}    

Sample Output:

20
10
0

Flowchart:

Flowchart: C# Sharp Exercises - Find the pair of adjacent elements that has the highest product of an given array of integers

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# program to check if a given string is a palindrome or not.
Next: Write a C# program which will accept a list of integers and checks how many integers are needed to complete the range.

What is the difficulty level of this exercise?



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