C++
while loop Exercise
Write a program that continues to ask the user to enter any set of numbers, until the user enters the number -1. Then display the total sum of numbers entered and their average. (note that you need to define a counter that counts how many numbers so the average = (sum/n) where n is your counter total.
#include <iostream> using namespace std; int main() { int number, n=0, sum=0; cout << "Enter a number to start with " << endl; cin >> number; while (number != -1) { . . . } . . . return 0; } |
#include<iostream>
using namespace std;
int main(){
int number,n=0,sum=0;
cout<<"Enter a number to start with
"<<endl;
cin>>number;
while(number!=-1){
sum=sum+number;
n=n+1;
cin>>number;
}
cout<<"sum of numbers:
"<<sum<<endl;
cout<<"Average of numbers:
"<<(sum/n)<<endl;
return 0;
}
Expected output:
Get Answers For Free
Most questions answered within 1 hours.