Sum the numbers entered using while loop - C++ Statement

C++ examples for Statement:while

Description

Sum the numbers entered using while loop

Demo Code

#include <iostream>
#include <iomanip>
using namespace std;
int main()/* w w w .  j  a v  a  2  s.  com*/
{
   const int MAXNUMS = 4;
   int count;
   double num, total;
   cout << "\nThis program will ask you to enter "
   << MAXNUMS << " numbers.\n";
   count = 1;
   total = 0;
   while (count <= MAXNUMS)
   {
      cout << "\nEnter a number: ";
      cin  >> num;
      total = total + num;
      cout << "The total is now " << setprecision(7) << total;
      count++;
   }
   cout << "\nThe final total is " << setprecision(7) << total << endl;
   return 0;
}

Result


Related Tutorials