w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Find the pair of adjacent elements that has the largest product of an given array which is equal to a given value

C# Sharp Basic: Exercise-55 with Solution

Write a C# program to find the pair of adjacent elements that has the largest product of an given array which is equal to a given value.

Sample Solution:

C# Sharp Code:

using System;
public class Exercise
    {
  public static int array_adjacent_elements_product(int[] input_array)
    {
    int array_index = 0;
    int product = input_array[array_index] * input_array[array_index + 1];

    array_index++;
    while (array_index + 1 < input_array.Length)
    {
        product = ((input_array[array_index] * input_array[array_index + 1]) > product) ?
                   (input_array[array_index] * input_array[array_index + 1]) :
                    product;
        array_index++;
    }

    return product;
}
        public static void Main()
        {
            Console.WriteLine(array_adjacent_elements_product(new int[] { 2, 4, 2, 6, 9, 3 }) == 27);
            Console.WriteLine(array_adjacent_elements_product(new int[] { 0, -1,-1, -2 }) == 2);
            Console.WriteLine(array_adjacent_elements_product(new int[] { 6, 1, 12, 3, 1, 4 }) == 36);
            Console.WriteLine(array_adjacent_elements_product(new int[] { 1, 4, 3, 0 }) == 16);
        }
    }

Sample Output:

False
True
True
False

Flowchart:

Flowchart: C# Sharp Exercises - Find the pair of adjacent elements that has the largest product of an given array which is equal to a given value.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# program to get the century from a year.
Next: Write a C# program to check if a given string is a palindrome or not.

What is the difficulty level of this exercise?



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