C++ Program to Calculate Average of Numbers

How to Calculate Average in C++
Formula
Average = Sum of numbers / Total count
To calculate the average in C++, first add all the numbers, then divide the sum by how many numbers are present. This is the basic logic used in every C++ average program.
C++ Program to Calculate Average of Two Numbers
This is the simplest case and is useful for beginners learning input and output in C++.
Here, we divide by 2.0 to avoid integer division and get the correct average.
C++ Program to Calculate Average of N Numbers
This approach works when the number of inputs is not fixed.
This program uses a loop to calculate the sum, then divides it by n to find the average of numbers in C++.
C++ Program to Find Average of an Array
Arrays are commonly used in beginner programs and interviews.
This program calculates the average of an array in C++ by traversing each element once.
C++ Average Using std::accumulate (Recommended)
This is a modern and cleaner way to calculate the average using STL
std::accumulate is preferred because it is readable, less error-prone, and widely used in modern C++ programs.
Average of Vector in C++
Vectors are dynamic and commonly used instead of arrays.
This example clearly shows how to calculate average using vector in C++.
Ready to Master C++ and Software Development?
Build a strong foundation in programming and crack top tech interviews with Scaler's Software Development Course.
Common Mistakes While Calculating Average in C++
Integer Division Leading to Incorrect Results
In C++, if both the numerator and denominator are integers, the result of division is also an integer. This means the decimal part is removed.
Example:
int sum = 5;
int count = 2;
float average = sum / count; // result is 2, not 2.5
Because both sum and count are integers, C++ performs integer division first. To fix this, at least one value must be a float.
Correct approach:
float average = sum / 2.0;
Forgetting to Type Cast Sum or Divisor to Float
Even if the result is stored in a float, C++ still performs integer division unless you explicitly convert one operand.
Example:
int sum = 7;
int n = 3;
float average = (float)(sum / n); // still wrong
Here, division happens before casting. The correct way is to cast before dividing.
Correct approach:
float average = (float)sum / n;
This ensures floating point division and gives the correct average.
Division by Zero When Array or Vector Is Empty
If an array or vector has zero elements, dividing by its size causes a runtime error.
Example:
vector<int> v;
float average = sum / v.size(); // division by zero
This leads to undefined behavior or program crash. Always check the size before dividing.
Safe approach:
if (v.size() > 0) {
float average = (float)sum / v.size();
}
Overflow When Sum Exceeds Integer Limit
If the sum of numbers is very large, it may exceed the maximum value an int can store, causing overflow.
Example:
int sum = 0;
for (int i = 0; i < 100000; i++) {
sum += 100000;
}
This can produce incorrect results without any error message. To avoid this, use a larger data types.
Correct approach:
long long sum = 0;
Using long long ensures accurate calculation of the average for large datasets.
Time and Space Complexity
| Scenario | Time Complexity | Space Complexity |
|---|---|---|
| Two numbers | O(1) | O(1) |
| N numbers using loop | O(n) | O(1) |
| Array average | O(n) | O(1) |
| Vector with accumulate | O(n) | O(1) |
FAQ
How to calculate average in C++?
To calculate average in C++, add all the numbers to get the sum, then divide the sum by the total count of numbers. Use float or double to avoid integer division errors.
What is the formula for average?
The formula for average is sum of all values divided by the total number of values. In C++, it is written as average = sum / count.
How to find average of an array in C++?
Traverse the array using a loop, add each element to a sum variable, then divide the sum by the array size to get the average of an array in C++.
How to calculate average using vector in C++?
You can calculate the average of a vector in C++ by summing all elements using a loop or std::accumulate, then dividing by the vector size.
Why does integer division give wrong average in C++?
Integer division truncates decimal values. If both sum and count are integers, the result will be an integer. Using float or double fixes this issue.