Q:

Write a C program that read 5 numbers and counts the number of positive numbers and print the average of all positive values

0

Write a C program that read 5 numbers and counts the number of positive numbers and print the average of all positive values

Test Data :
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
Expected Output:
Number of positive numbers: 4
Average value of the said positive numbers: 12.00

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
int main() {
	float numbers[5],total=0, avg;
	int j, pctr=0; 
	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)
		{
			pctr++;
			total += numbers[j];
		}	
    }
    avg = total/pctr;
	printf("\nNumber of positive numbers: %d", pctr);
	printf("\nAverage value of the said positive numbers: %.2f", avg);
	printf("\n");
	return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now