Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program which prompts the user to enter some numbers and calculates

ID: 3792703 • Letter: W

Question

Write a C++ program which prompts the user to enter some numbers and calculates the average of the numbers entered. Entering 0 should terminate the sequence of numbers and cause the calculated average to be displayed.

Requirements:

The user can enter numbers with or without a fractional part.

The program assumes that only numbers greater than or equal to zero are entered by the user. (The program will be tested using this assumption).

The number of positive numbers that the user can enter must be unlimited.

The prompts and output displayed by the program must be formatted as shown in the sample runs below (e.g. blank lines, spacing, case of text).

A sample run of your program should look like: Entering 0 will terminate the sequence of input values.

Enter a number: 4.5

Enter a number: 12

Enter a number: 8.1

Enter a number: 9

Enter a number: 17

Enter a number: 3.2

Enter a number: 0

The average of the numbers you entered is 8.9667

Explanation / Answer

#include<bits/stdc++.h>
using namespace std;


int main(int argc, char const *argv[])
{
  
float n=1;
float sum=0,c=0;
while(n>0){
       cout<<"Enter a number :";
       cin>>n;
       c++;
       sum+=n;

}

cout<<" The average of the numbers you entered is "<<(float)sum/(c-1);

   return 0;
}

===================================================================

Enter a number: 4.5

Enter a number: 12

Enter a number: 8.1

Enter a number: 9

Enter a number: 17

Enter a number: 3.2

Enter a number: 0

The average of the numbers you entered is 8.9667

========================================================

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote