📊⚡Beginner
Find Maximum and Minimum in Array
Find both min and max
Finding the minimum and maximum elements in an array requires traversing all elements and keeping track of the smallest and largest values found.
C Program to Find Min and Max in Array
array_min_max.c
C
1#include <stdio.h>23int main() {4 int arr[] = {45, 12, 67, 23, 89, 34, 56};5 int n = sizeof(arr) / sizeof(arr[0]);6 7 // Initialize min and max with first element8 int min = arr[0];9 int max = arr[0];10 11 // Find min and max12 for (int i = 1; i < n; i++) {13 if (arr[i] < min) {14 min = arr[i];15 }16 if (arr[i] > max) {17 max = arr[i];18 }19 }20 21 printf("Array: ");22 for (int i = 0; i < n; i++) {23 printf("%d ", arr[i]);24 }25 printf("\nMinimum element: %d\n", min);26 printf("Maximum element: %d\n", max);27 28 return 0;29}Output
Array: 45 12 67 23 89 34 56
Minimum element: 12
Maximum element: 89
📋 Algorithm
- Initialize min and max with the first element
- Traverse array from index 1 to n-1
- If current element < min, update min
- If current element > max, update max
🎯 Key Takeaways
✓Initialize min/max with first element
✓Time complexity: O(n)
✓Single pass through array
✓sizeof finds array length
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials