Calculate total using while loop and calculate average - C++ Statement

C++ examples for Statement:while

Description

Calculate total using while loop and calculate average

Demo Code

#include <iostream>
#include <iomanip>
using namespace std;
int main()//from  w ww. ja  v a 2 s  . c  o m
{
   const int MAXNUMS = 4;
   int count;
   double num, total, average;
   cout << "\nThis program will ask you to enter " << MAXNUMS << " numbers.\n";
   count = 1;
   total = 0;
   while (count <= MAXNUMS)
   {
      cout << "Enter a number: ";
      cin  >> num;
      total = total + num;
      count++;
   }
   count--;
   average = total / count;
   cout << "\nThe average of the numbers is " << average << endl;
   return 0;
}

Result


Related Tutorials