c++ average
To calculate the average of a set of numbers in C++, you can follow these steps:
- Declare variables to store the sum of the numbers and the count of numbers.
- Prompt the user to input the numbers and store them in variables.
- Add each input number to the sum variable and increment the count variable.
- Calculate the average by dividing the sum by the count.
- Display the average to the user.
Here's an example of C++ code to achieve this:
#include <iostream>
using namespace std;
int main() {
int count, num;
double sum = 0;
cout << "Enter the number of elements: ";
cin >> count;
for (int i = 0; i < count; i++) {
cout << "Enter number " << i + 1 << ": ";
cin >> num;
sum += num;
}
double average = sum / count;
cout << "The average is: " << average;
return 0;
}