. Suppose you want to read some positive integers and average them, but you do not have a preset number of data items in mind. Suppose the number -999 marks the end of the data. Find the sum and the average of these numbers
//Program reads some positive integers and average them
// Find the sum and the average of these numbers
Â
// the preprocessing directives
#include <iostream>
using namespace std;
Â
int main() {
// declare and initialize variables
  int i=0, count=0, sum=0;
  double avg;
Â
  // loop while !=-999
   while(1){
        cout << "i = ";
        cin >> i;
        if(i!=-999){
               sum = sum+i;
               count++;
        }
        else
               break;
   }
   avg = double(sum)/double(count);
Â
   cout<<"\nSUM = "<<sum;
   cout<<"\nAVG = "<<avg;  Â
Â
   cout<<endl;
   return 0;
}
Comments
Leave a comment