When working with arrays in C, finding the maximum element is a fundamental operation that helps improve your understanding of loops and data storage. In this article, we’ll explore how to write a program to find maximum element from 1-Dimensional array in C, including the complete code and explanation.
C Program to Find Maximum Element from 1-Dimensional Array
#include <stdio.h>
int main() {
int arr[100], n, i, max;
printf("Enter number of elements in array: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
max = arr[0];
for(i = 1; i < n; i++) {
if(arr[i] > max) {
max = arr[i];
}
}
printf("Maximum element = %d\n", max);
return 0;
}
Output Example
Enter number of elements in array: 5
Enter 5 elements:
10 35 22 87 56
Maximum element = 87
Explanation
- We take input for the size of the array and its elements.
- We initialize the first element as the maximum and iterate through the rest.
- If a larger value is found, it replaces the current maximum.
- This logic uses basic loops and comparisons which are essential in array-based problems.