In: Computer Science
Write a program that uses a while statement to read integers from the user and prints the sum, average, and largest of these numbers. The input should terminate if the user enters 999. The average should be output with 4 digits of precision.
c++ please ASAP
Program:
#include <iostream>
using namespace std;
int main()
{
int intiger, large = 0, count = 0;
float sum = 0, avg;
// start while loop
while (true)
{
// read intiger from keyboard
cout << "Enter intiger (to stop enter 999)";
cin >> intiger;
// check the enterd input is 999
if (intiger == 999) {
// then exit from the while loop
break;
}
// increment the count
count++;
// add the enter intiger to sum variable
sum = sum + intiger;
// check the current Largest is less than readed intiger
if(large < intiger)
{
// then chage the largest
large = intiger;
}
}
// find the average
avg = sum / count;
// print the sum
printf("Sum: %.4f \n", sum);
// print the Largest
printf("Largest: %d \n", large);
// print the Average
printf("Average: %.4f", avg);
return 0;
}
Screenshot:
.