Tuesday, March 24, 2026

Write a program to find the minimum, maximum and average in an array of integers || C Lab Program

 WAP_C01: Write a C program to find the minimum, maximum and average in an array of integers..|| Arrays, Pointers and Functions


WAP_C01: C Lab Program


//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



For all 2026 published articles list: click here

...till the next post, bye-bye & take care.

No comments:

Post a Comment