w3resource

C Exercises: Display the number of positive values, the minimum value, the maximum value and the average of all numbers


Count positive integers and display their min, max, and average

Write a C program that accepts integers from the user until a zero or a negative number, displays the number of positive values, the minimum value, the maximum value, and the average value.

Test data and expected output:
5 2 5 8 9 12 0

Pictorial Presentation:

C Programming: Display the number of positive values, the minimum value, the maximum value and the average of all numbers.


Sample Solution:

C Code:

#include <stdio.h>
int main() {
    int a, ctr = 0, min_num, max_num, s = 0;
    double avg;

    // Prompt user for input
    printf("Input a positive integer:\n");

    // Read the first input value
    scanf("%d", &a);

    // Check if the input is positive
    if (a <= 0) {
        printf("No positive number entered\n");
        return 0;
    }

    // Initialize min_num and max_num with the first input value
    min_num = a;
    max_num = a;

    // Loop to process subsequent inputs
    while (a > 0) {
        s += a;
        ctr++;
        max_num = a > max_num ? a : max_num;
        min_num = a < min_num ? a : min_num;

        // Prompt for the next positive integer
        printf("Input next positive integer:\n");
        scanf("%d", &a);
    }

    avg = s / (double) ctr;

    // Display the results
    printf("Number of positive values entered is %d\n", ctr);
    printf("Maximum value entered is %d\n", max_num);
    printf("Minimum value entered is %d\n", min_num);
    printf("Average value is %0.4lf\n", avg);

    return 0;
}

Sample Output:

Input a positive integer:
Input next positive integer: 15
Input next positive integer: 25
Input next positive integer: 37
Input next positive integer: 43
Number of positive values entered is 4
Maximum value entered is 43
Minimum value entered is 15
Average value is 30.0000

Flowchart:

C Programming Flowchart: Display the number of positive values, the minimum value, the maximum value and the average of all numbers


For more Practice: Solve these Related Problems:

  • Write a C program to count positive integers from user input until a sentinel value is encountered and display the min, max, and average.
  • Write a C program to compute the average of only the prime numbers entered by the user.
  • Write a C program to calculate the minimum, maximum, and average of even positive integers from a list.
  • Write a C program to compute min, max, and average using dynamic memory allocation for user-input numbers.

Go to:


PREV :Sum the series 1^4+2^4+4^4+...+m^4.
NEXT : Print prime numbers between 1 and 200 in rows of 20.

C programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.