Question

(C++ Microsoft Visual Studio) [15 Points] Write a while loop to calculate the average of numbers...

(C++ Microsoft Visual Studio) [15 Points] Write a while loop to calculate the average of numbers entered by the user:  Ask the user to enter an integer number or -1 to stop  Add the entered numbers  Compute the average of the numbers outside the loop after termination  Print the average

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;
int main(){
   float sum = 0, n;
   int count = 0;
   
   cout<<"Enter value: ";
   cin>>n;
   
   while(n!=-1){
       sum += n;
       count += 1;
       cout<<"Enter value: ";
       cin>>n;
   }
   
   cout<<"Average = "<<(sum/count)<<endl;
   return 0;
}

Output:

Enter value: 4 Enter value: 2 Enter value: 3 Enter value: 1 Enter value: 5 Enter value: -1 Average = 3

Add a comment
Know the answer?
Add Answer to:
(C++ Microsoft Visual Studio) [15 Points] Write a while loop to calculate the average of numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT