0
0
CppProgramBeginner · 2 min read

C++ Program to Find Average of Numbers

To find the average of numbers in C++, sum all numbers using a loop and then divide by the count using average = sum / count;.
📋

Examples

Input3 numbers: 10, 20, 30
OutputAverage is 20
Input5 numbers: 5, 15, 25, 35, 45
OutputAverage is 25
Input1 number: 100
OutputAverage is 100
🧠

How to Think About It

To find the average, first add all the numbers together to get the total sum. Then count how many numbers you have. Finally, divide the total sum by the count to get the average.
📐

Algorithm

1
Get the number of values to average.
2
Initialize a sum variable to zero.
3
Loop through each number, adding it to the sum.
4
Divide the sum by the count of numbers to get the average.
5
Print the average.
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of values: ";
    cin >> n;
    double sum = 0, num;
    for (int i = 0; i < n; i++) {
        cout << "Enter number " << i + 1 << ": ";
        cin >> num;
        sum += num;
    }
    double average = sum / n;
    cout << "Average is " << average << endl;
    return 0;
}
Output
Enter number of values: 3 Enter number 1: 10 Enter number 2: 20 Enter number 3: 30 Average is 20
🔍

Dry Run

Let's trace the example with input numbers 10, 20, 30 through the code.

1

Input number count

User inputs n = 3

2

Initialize sum

sum = 0

3

First iteration

Input num = 10; sum = 0 + 10 = 10

4

Second iteration

Input num = 20; sum = 10 + 20 = 30

5

Third iteration

Input num = 30; sum = 30 + 30 = 60

6

Calculate average

average = sum / n = 60 / 3 = 20

7

Output result

Print "Average is 20"

IterationInput NumberSum So Far
11010
22030
33060
💡

Why This Works

Step 1: Summing numbers

We add each input number to a running total stored in sum to get the combined value.

Step 2: Counting inputs

We keep track of how many numbers we have with n to know how many to divide by.

Step 3: Calculating average

Dividing the total sum by the count gives the average value of the numbers.

🔄

Alternative Approaches

Using array to store numbers
cpp
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of values: ";
    cin >> n;
    double* numbers = new double[n];
    double sum = 0;
    for (int i = 0; i < n; i++) {
        cout << "Enter number " << i + 1 << ": ";
        cin >> numbers[i];
        sum += numbers[i];
    }
    double average = sum / n;
    cout << "Average is " << average << endl;
    delete[] numbers;
    return 0;
}
Stores all numbers in a dynamically allocated array, useful if you need to reuse them later but uses more memory.
Using while loop
cpp
#include <iostream>
using namespace std;

int main() {
    int n, count = 0;
    double sum = 0, num;
    cout << "Enter number of values: ";
    cin >> n;
    while (count < n) {
        cout << "Enter number " << count + 1 << ": ";
        cin >> num;
        sum += num;
        count++;
    }
    double average = sum / n;
    cout << "Average is " << average << endl;
    return 0;
}
Uses a while loop instead of for loop, functionally similar but different style.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops through all n numbers once to sum them, so time complexity is O(n).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar time complexity; using arrays uses more space but allows reuse of numbers.

ApproachTimeSpaceBest For
Simple loop with sumO(n)O(1)Quick average calculation without storing numbers
Array storageO(n)O(n)When you need to access numbers later
While loopO(n)O(1)Alternative loop style, same performance
💡
Always use a double type for sum and average to handle decimal values correctly.
⚠️
Dividing integers without converting to double can cause incorrect average due to integer division.