forked from Nikhil-2002/Programming_Hactoberfest25
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargestelement.c
More file actions
35 lines (27 loc) · 739 Bytes
/
Copy pathlargestelement.c
File metadata and controls
35 lines (27 loc) · 739 Bytes
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
#include <stdio.h>
int main() {
int n;
// Taking the size of the array from the user
printf("Enter the size of the array: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid size\n");
return 1; // Exit the program with an error code
}
int arr[n];
// Taking array elements as input from the user
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
}
// Finding the largest element
int max = arr[0];
for (int i = 1; i < n; ++i) {
if (arr[i] > max) {
max = arr[i];
}
}
// Displaying the result
printf("The largest element in the array is: %d\n", max);
return 0;
}