# C Exercises: Counts the number of positive numbers and print the average of all positive values

## C Basic Declarations and Expressions: Exercise-28 with Solution

Write a C program that reads 5 numbers, counts the number of positive numbers, and prints out the average of all positive values.

**Pictorial Presentation:**

**C Code:**

```
#include <stdio.h>
int main() {
float numbers[5], total=0, avg; // Declare an array to store 5 numbers, and variables for total and average
int j, pctr=0; // Declare variables for loop counter and positive count
// Prompt user for five numbers and store them in the array
printf("\nInput the first number: ");
scanf("%f", &numbers[0]);
printf("\nInput the second number: ");
scanf("%f", &numbers[1]);
printf("\nInput the third number: ");
scanf("%f", &numbers[2]);
printf("\nInput the fourth number: ");
scanf("%f", &numbers[3]);
printf("\nInput the fifth number: ");
scanf("%f", &numbers[4]);
for(j = 0; j < 5; j++) {
if(numbers[j] > 0) // Check if the number is positive
{
pctr++; // Increment positive count
total += numbers[j]; // Add positive number to total
}
}
avg = total/pctr; // Calculate average of positive numbers
// Print the counts of positive numbers and the average
printf("\nNumber of positive numbers: %d", pctr);
printf("\nAverage value of the said positive numbers: %.2f", avg);
printf("\n");
return 0;
}
```

Sample Output:

Input the first number: 5 Input the second number: 8 Input the third number: 10 Input the fourth number: -5 Input the fifth number: 25 Number of positive numbers: 4 Average value of the said positive numbers: 12.00

**Flowchart:**

**C Programming Code Editor:**

**Previous:** Write a C program that read 5 numbers and counts the number of positive numbers and negative numbers.

**Next:** Write a C program that read 5 numbers and sum of all odd values between them.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.**

https://www.w3resource.com/c-programming-exercises/basic-declarations-and-expressions/c-programming-basic-exercises-28.php

**Weekly Trends and Language Statistics**- Weekly Trends and Language Statistics