Answer to Question #174773 in C++ for Tanjunxi

Question #174773

Write a do-while loop that repeatedly prompts for and intakes input until a negative value is entered. The program will end and display the average of the values entered once a negative value is entered.




1
Expert's answer
2021-03-23T14:35:31-0400
#include <iostream>
using namespace std;

int main() {
    int number = 0;
    int sum = 0;
    float av;

    do {
        sum += number;

        // take input from the user
        cout << "Enter a number: ";
        cin >> number;
    }
    while (number >= 0);
   
    // display the sum
    cout << "\nThe sum is " << sum << endl;
      av=(float) sum/number;
      cout<<"\n Average is :"<<av;
   
    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
New on Blog
APPROVED BY CLIENTS