Write a do-while loop that repeatedly prompts for and intakes input until a negative value is entered. The program will end and display the average of the values entered once a negative value is entered.
#include <iostream>
using namespace std;
int main() {
int number = 0;
int sum = 0;
float av;
do {
sum += number;
// take input from the user
cout << "Enter a number: ";
cin >> number;
}
while (number >= 0);
// display the sum
cout << "\nThe sum is " << sum << endl;
av=(float) sum/number;
cout<<"\n Average is :"<<av;
return 0;
}
Comments
Leave a comment