Write a C program to find the minimum, maximum and average in an array of integers.
Write a C program to find the minimum, maximum and average in an array of integers.
Algorithm
start
step1:
step2:
step3:
step4:
step5:
step6:
step7:
step8:
step9:
step10:
stop
Flowchart
C Programming
//to find the minimum, maximum and average in an array of integers
#include <stdio.h>
#include <math.h>
int main() {
int n,arr[10],min,max,sum;
float average;
// Read the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
// Read the elements of the array
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Initialize min, max and sum
min = arr[0];
max = arr[0];
sum = arr[0]; // Using long long to avoid overflow
// Iterate through the array to find min, max, and sum
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
sum += arr[i];
}
// Calculate the average
average = (float)sum / n;
// Print the results
printf("Minimum: %d\n", min);
printf("Maximum: %d\n", max);
printf("Average: %.2f\n", average);
return 0;
}
OUTPUT
Enter the number of elements in the array: 5
Enter 5 integers:
5
15
25
10
20
Minimum: 5
Maximum: 25
Average: 15.00
๐ป Run This C Program
Write a C program to find the minimum, maximum and average in an array of integers.
The selected C program is loaded automatically. Edit the code,
provide input through STDIN and click Run.
The compiler is embedded using OneCompiler. Your program runs in
the embedded C environment; Study Glance does not execute C code
directly on your web server.