Write a program that repeatedly asks the user to enter a number, either float or
ID: 3701687 • Letter: W
Question
Write a program that repeatedly asks the user to enter a number, either float or integer until a value -88 is entered. The program should then output the average of the numbers entered with two decimal places. Please note that -88 should not be counted as it is the value entered to terminate the loop. (5) A sample run: Enter a number(integer or float):5 Enter a number(integer or float):3.2 Enter a number(integer or float):2.1 Enter a number(integer or float)-88 The average of 5 numbers entered is 3.43 Step 1: To repeatedly asks the user to enter a number, you will need a WHILE loop, -88 should be used as the condition to stop the while loop Step 2: To calculate the average of the numbers, you will need to declare a COUNTER and a TOTAL and a NUMBER for the user input, before the WHILE loop starts Step 3: Inside the WHILE loop TOTAL should be calculated as TOTAL +NUMBER, and COUNTER should be incremented by 1 each time Step 4: Once WHILE loop stops, Average should be calculated as TOTAL/ COUNTER Step 5: Print out the resultExplanation / Answer
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
float Counter=0,Total=0;
float num;
float avg;
cout<<"Enter a Number(integer or float):";
while(cin>>num)
{
if(num!=-88){
Total=Total+num;
Counter++;
}else{
break;
}
cout<<" Enter a Number(integer or float):";
}
avg=Total/Counter;
cout<<" The average of "<<Counter<<" numbers is ";
cout << fixed << showpoint;
cout << setprecision(2);
cout << avg << endl;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.