﻿ C# - Find maximum and minimum element in an array

# C#: Find maximum and minimum element in an array

## C# Sharp Array: Exercise-9 with Solution

Write a C# Sharp program to find the maximum and minimum elements in an array.

Sample Solution:-

C# Sharp Code:

``````using System;

public class Exercise9
{
public static void Main()
{
int[] arr1 = new int[100];  // Array to store elements
int i, mx, mn, n;  // Variables for maximum, minimum, and size of array

Console.Write("\n\nFind maximum and minimum element in an array :\n");
Console.Write("--------------------------------------------------\n");

Console.Write("Input the number of elements to be stored in the array :");

Console.Write("Input {0} elements in the array :\n", n);
for (i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i);
}

// Initializing maximum and minimum with the first element of the array
mx = arr1[0];
mn = arr1[0];

// Finding maximum and minimum elements
for (i = 1; i < n; i++)
{
if (arr1[i] > mx)  // If current element is greater than the current maximum
{
mx = arr1[i];  // Update maximum
}

if (arr1[i] < mn)  // If current element is smaller than the current minimum
{
mn = arr1[i];  // Update minimum
}
}

// Display maximum and minimum elements
Console.Write("Maximum element is : {0}\n", mx);
Console.Write("Minimum element is : {0}\n\n", mn);
}
}
```
```

Sample Output:

```Find maximum and minimum element in an array :
--------------------------------------------------
Input the number of elements to be stored in the array :2
Input 2 elements in the array :
element - 0 : 20
element - 1 : 25
Maximum element is : 25
Minimum element is : 20
```

Flowchart:

C# Sharp Code Editor: