Write a Program to Find Maximum Element from 1-Dimensional Array in C

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.

Previous Article

Read and Store Roll No and Marks of 20 Students Using Array in C

Next Article

Write a C Program to Calculate the Average, Geometric and Harmonic Mean of n Elements in an Array

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨