In: Computer Science
Write a program to print the number of class assignments you have done so far, the total grades, and average grade. Prompt a 0 input to end the program.
Java
Using while statements in the lecture this was provided. Wording confused me, Total grade is sum of all grades.
/*******************************************************/
#include <iostream>
using namespace std;
int main()
{
// Declaring variables
int grade, tot = 0, count = 0;
double avg = 0.0;
/* This while loop continues to execute
* until the user enter grade as 0
*/
while (true)
{
/* This while loop continues to execute
* until the user chooses valid grade
*/
while (true)
{
cout << "Enter grade (0 to exit):";
cin >> grade;
if (grade == 0)
{
break;
}
else if (grade < 0 || grade > 100)
{
cout << "** Invalid.Must be between 1 - 100 **" <<
endl;
}
else
{
break;
}
}
if (grade == 0)
{
break;
}
else
{
// Calculating the sum of all grades
tot += grade;
// Counting no of grades
count++;
}
}
// Calculating the average
avg = ((double)tot / count);
// displaying the output
cout << "Sum of all grades :" << tot <<
endl;
cout << "Average of all grades :" << avg <<
endl;
return 0;
}
/*******************************************************/
/*******************************************************/
Output:
/*******************************************************/