Program to find the minimum (or maximum) element of an array in C#
- Get link
- X
- Other Apps
Solution 1:
// C# code for the approach
using System;
class GFG {
// Driver Code
static void Main()
{
// Input array
int[] arr = { 1, 423, 6, 46, 34, 23, 13, 53, 4 };
int n = arr.Length;
// Sort the array
Array.Sort(arr);
// after sorting the value at 0th position will be
// minimum and nth position will be maximum
Console.WriteLine("min-" + arr[0] + " max-"
+ arr[n - 1]);
}
}
Output
min-1 max-423
Solution 2:
// C# code for the approach
using System;
class GFG {
// Driver Code
static void Main()
{
// Input array
int[] arr = { 1, 423, 6, 46, 34, 23, 13, 53, 4 };
int n = arr.Length;
int min =0, max =0;
int length = arr.Length;
for (int i = 0; i < length; i++)
{
int crr = arr[i];
if (i == 0)
{
min = crr;
max = crr;
}
else
{
min = crr < min ? crr : min;
max = crr > max ? crr : max;
}
}
Console.WriteLine("Min: " + min+ ", Max: " + max);
}
}
Output
min-1 max-423
- Get link
- X
- Other Apps
Comments
Post a Comment