Computes a grade average with a for loop. Read the grade from user - C++ Statement

C++ examples for Statement:for

Description

Computes a grade average with a for loop. Read the grade from user

Demo Code

#include <iostream>
using namespace std;
#include <iomanip>
int main()//www  .  j av  a2  s . c o  m
{
   float grade, avg;
   float total=0.0;
   int num;                 
   int loopvar;             

   cout << "How many students are there? ";
   cin >> num;     // Get total number to enter
   for (loopvar=1; loopvar<=num; loopvar++)
   {
      cout << "\nWhat is the next student's grade? ";
      cin >> grade;
      total += grade;
   }     // Keep a running total
   avg = total / num;
   cout << "\n\nThe average of this class is " << setprecision(1) << avg;
   return 0;
}

Result


Related Tutorials