C++ Program to Calculate Arithmetic Mean

In this article, you will learn and get code on finding the arithmetic of all the numbers entered by the user in C++. The user is also allowed to define the size. For example, if the user enters 5 as the size, then the program further asks for any 5 numbers to find the arithmetic mean. There are two programs available here:

How to find the arithmetic mean

If there are n sets of numbers, we have a formula for calculating the arithmetic mean:

am = (n1+n2+n3+...+nn)/n

Here am indicates the arithmetic mean, whereas n1, n2, n3, and nn indicate the first, second, third, and last number, respectively.

For example, if we have five sets of numbers, say 10, 20, 30, 40, and 50, we can calculate their arithmetic mean as follows:

am = (10+20+30+40+50)/5
   = 150/5
   = 30

Now let's move on to the program.

In C++, find the Arithmetic Mean

In order to calculate (or find) the arithmetic mean (of numbers) in C++ programming, you must first ask the user to enter the size (how many sets of numbers) and then ask the user to enter all numbers of that size in order to find and print the arithmetic mean.

To calculate the arithmetic mean of numbers, first perform the addition of all the numbers, then make a variable  responsible for the arithmetic mean and place addition or size in a variable saying "armean" (arithmetic mean), then display the result on the output screen as shown here in the following program.

#include<iostream>
using namespace std;
int main()
{
    int n, i;
    float arr[50], sum=0, armean;
    cout<<"How many number, You want to enter ? ";
    cin>>n;
    cout<<"\nEnter "<<n<<" Number: ";
    for(i=0; i<n; i++)
    {
        cin>>arr[i];
        sum = sum+arr[i];
    }
    armean = sum/n;
    cout<<"\nArithmetic Mean = "<<armean;
    cout<<endl;
    return 0;
}

This program was built and runs under the Code::Blocks IDE. Here is its sample run:

C++ program arithmetic mean

Now enter the size, which is how many numbers you want to enter to find the arithmetic mean. Let's suppose, user enters the size as 5 and the 5 numbers as 10, 2, 4, 6, and 12. Press the ENTER key to see the following output:

c++ find arithmetic mean

When we received the numbers, we added them one by one to the total. For instance, if the user enters 5 as the size and the numbers 10, 2, 4, 6, and 12, the dry run (within the for loop) will look like this:

In C++, find the arithmetic mean using the function

This function does the same job as the previous program. But it is created using functions. That is, a function like arithmeticMean() takes two arguments: the first one is the array, and the second is its size. and returns the arithmetic mean of all numbers stored in the array.

#include<iostream>
using namespace std;
float arithmeticMean(float [], int);
int main()
{
    int n, i;
    float arr[50], armean;
    cout<<"Enter the Size (maz. 50): ";
    cin>>n;
    cout<<"\nEnter "<<n<<" Numbers: ";
    for(i=0; i<n; i++)
        cin>>arr[i];
    armean = arithmeticMean(arr, n);
    cout<<"\nArithmetic Mean = "<<armean;
    cout<<endl;
    return 0;
}
float arithmeticMean(float arr[], int n)
{
    int i;
    float sum=0, am;
    for(i=0; i<n; i++)
        sum = sum+arr[i];
    am = sum/n;
    return am;
}

Here is its sample run with user input of 2 as size and 10, 3, as two numbers:

arithmetic mean using function c++

The same program in different languages

C++ Quiz


« Previous Program Next Program »