C Online Compiler
Example: Find Min Max using Simple Iteration in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Find Min Max using Simple Iteration #include <stdio.h> int main() { // Step 1: Declare and initialize an array int arr[] = {12, 5, 8, 20, 3, 15, 7}; 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 if (arr[i] < min_val) { min_val = arr[i]; } // Step 5: Compare current element with max_val 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("Minimum element: %d\n", min_val); printf("Maximum element: %d\n", max_val); return 0; }
Output
Clear
ADVERTISEMENTS