Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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

C Basic Declarations and Expressions: Exercise-64 with Solution

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

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;
    printf("Input a positive integer:\n");
    scanf("%d", &a);
    if (a <= 0) 
    {
      printf("No positive number entered\n");
      return 0;
    }
    min_num = a;
    max_num = a;
    while (a > 0) {
      s += a;
      ctr++;
      max_num = a > max_num ? a : max_num;
      min_num = a < min_num ? a : min_num;
      printf("Input next positive integer:\n");
      scanf("%d", &a);
    }
    avg = s / (double) ctr;
    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

C programming Code Editor:

Contribute your code and comments through Disqus.

Previous:Write a C program that accepts a positive integer n less than 100 from the user and prints out the sum 14 + 24 + 44 + 74 + 114 + • • • + m4 , where m is less than or equal to n. Print appropriate message.
Next: Write a C program that prints out the prime numbers between 1 and 200. The output should be such that each row contains a maximum of 20 prime numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP