Answer to Question #71577 in C++ for m

Question #71577
Write a program which repeatedly reads numbers until the user enters 0. Once 0 is entered, print out the total, count, and average of the numbers. Use an array to store all the numbers. Example of execution:
Enter a number: 4
Enter a number: 5
Enter a number: 9
Enter a number: 0
Total = 18, count = 3, average= 6
answer by using while,for and array if possible
1
Expert's answer
2017-12-07T07:13:06-0500
#include <iostream>

using namespace std;

int main()
{
cout << "Input numbers.\n";
cout << "To finish entering input 0.\n";
int countEl = 0;
double sum = 0;
int element;
cin >> element;
while (element){
sum += element;
countEl++;
cin >> element;
};

cout << "Total = " << sum << endl;
cout << "Count = " << countEl << endl;
cout << "Average = " << double (sum / countEl) << endl;

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS