C Online Compiler
Example: Find Min Max in Array (Simple Iteration) in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Find Min Max in Array (Simple Iteration) #include <stdio.h> int main() { // Step 1: Declare and initialize an array int arr[] = {12, 5, 89, 3, 27, 45, 9}; int n = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements // Step 2: Initialize min_val and max_val with the first element int min_val = arr[0]; int max_val = arr[0]; // Step 3: Iterate through the rest of the array (from the second element) for (int i = 1; i < n; i++) { // Step 4: Compare current element with min_val and update if smaller if (arr[i] < min_val) { min_val = arr[i]; } // Step 5: Compare current element with max_val and update if larger if (arr[i] > max_val) { max_val = arr[i]; } } // Step 6: Print the results printf("Array elements: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); printf("Smallest element: %d\n", min_val); printf("Largest element: %d\n", max_val); return 0; }
Output
Clear
ADVERTISEMENTS