C++ for Beginners: C++ Program to Calculate Average of Numbers Using Arrays

(C++ programming Example for Beginners)

C++ Program to Calculate Average of Numbers Using Arrays

This program takes n number of element from user (where, n is specified by user), stores data in an array and calculates the average of those numbers.

Example: Calculate Average of Numbers Using Arrays

#include <iostream>
using namespace std;

int main(){
    int n, i;
    float num[100], sum=0.0, average;

    cout << "Enter the numbers of data: ";
    cin >> n;

    while (n > 100 || n <= 0)
        cout << "Error! number should in range of (1 to 100)." << endl;
        cout << "Enter the number again: ";
        cin >> n;

    for(i = 0; i < n; ++i)
        cout << i + 1 << ". Enter number: ";
        cin >> num[i];
        sum += num[i];

    average = sum / n;
    cout << "Average = " << average;

    return 0;


Enter the numbers of data: 6
1. Enter number: 45.3
2. Enter number: 67.5
3. Enter number: -45.6
4. Enter number: 20.34
5. Enter number: 33
6. Enter number: 45.6
Average = 27.69

This program calculates the average if the number of data are from 1 to 100.

If user enters value of n above 100 or below 100 then, while loop is executed which asks user to enter value of n until it is between 1 and 100.

When the numbers are entered by the user, subsequently the sum is calculated and stored in the variable sum.

After storing all the numbers, average is calculated and displayed.


C++ for Beginners: C++ Program to Calculate Average of Numbers Using Arrays

Sign up to get end-to-end “Learn By Coding” example.

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.