forked from strivedi4u/hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shashank.cs
39 lines (34 loc) · 957 Bytes
/
shashank.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
class Program
{
static void Main(string[] args)
{
int[] arr = { 10, 5, 20, 8 };
try
{
int largestElement = FindLargestElement(arr);
Console.WriteLine("The largest element in the array is: " + largestElement);
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
}
static int FindLargestElement(int[] arr)
{
if (arr.Length == 0)
{
throw new ArgumentException("Array is empty.");
}
int largest = arr[0]; // Assume the first element is the largest
// Iterate through the array to find the largest element
foreach (int num in arr)
{
if (num > largest)
{
largest = num; // Update largest if current element is greater
}
}
return largest; // Return the largest element found
}
}