Write a program that reads a list of test scores and calculate their average. An input of EOF(by pressing CTRL+Z) for a score denotes end-of-data for the user

C Programming Language Loop control in C Language (Article) Loop control in C Language (Program)

972

Program:

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int n, sum, score;
	float average;
	sum = 0;
	n = 0;
	printf("\n Enter test scores one by one(EOF to quit): ");
	while(scanf("%d", &score) != EOF)
	{
		sum += score;
		n++;
	}
	average = (float)sum / n;
	printf("\n The average is %f", average);
	
     getch();
	return 0;
}

Output:

 Enter test scores one by one(EOF to quit): 10
20
30
40
^Z

 The average is 25.000000

Explanation:

None

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.