Unit 2 covered Chapters 4-7. Chapter 4 covered selection statements which includ
ID: 643129 • Letter: U
Question
Unit 2 covered Chapters 4-7. Chapter 4 covered selection statements which included if, if-else, and switch statements. Chapter 5 covered basic loop structures, such as while, for and do-while statements Chapters 6 and 7 covered functions. Refer to "What to Turn In" for specific details on what should be turned in and how to turn in your project.
Write a C program that accepts numeric grades and then displays the average of the grades entered. Sample output: To stop entering grades, type in any negative number.
Enter a grade: 98.4
Enter a grade: 99
Enter a grade: -1
The total of the grades is 197.40
The average of the grades is 98.70
Explanation / Answer
#include <stdio.h>
int main(){
float score = 0, val;
int num = 0;
while(1){
printf("Enter a grade: ");
scanf("%f", &val);
if(val < 0){
break;
}
score += val;
++num;
}
printf("The total of the grades is %.2f ", score);
if(num != 0)
printf("The average of the grades is %.2f ", score / num);
else
printf("The average of the grades is 0.00 ");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.