In: Computer Science
- Design and implement a function with no input parameters. The function keeps receiving a number from input (user) and adds the numbers together. The application keeps doing it until the user enter 0. Then the application will stop and print the total sum and average of the numbers the user had entered.
C++ code:
#include <iostream>
using namespace std;
void totalAndAvg()
{
int num;
int count = 0;
int sum = 0;
float avg;
while(1)
{
cout<<"Enter a number( 0 to Stop): ";
cin>>num;
if(num == 0)
break;
else
{
sum += num;
count++;
}
}
avg = (float) sum / count;
cout << "Sum = "<<sum<<endl;
cout << "Average = "<< avg <<endl;
}
int main() {
totalAndAvg();
return 0;
}
Screenshots: