codescracker
c++

C++ Program to Calculate Arithmetic Mean



« Previous Program Next Program »


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

How to Find Arithmetic Mean ?

If there are n set of numbers, therefore to find arithmetic mean, we have a formula:

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

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

For example, if we have 5 set of numbers say 10, 20, 30, 40, 50, then its arithmetic means can be calculated as:

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

Now let's move on to the program.

Find Arithmetic Mean in C++

To calculate (or find) arithmetic mean (of numbers) in C++ programming, you have to ask from user to enter the size (how many set of number), then ask to enter all numbers of that size to find and print arithmetic mean.

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

// C++ Program to Find Arithmetic Mean
// ----codescracker.com

#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 build and run under Code::Blocks IDE. Here is its sample run:

C++ program arithmetic mean

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

c++ find arithmetic mean

At the time of receiving numbers, we've added each, one by one to sum. For example, if user enters 5 as size, and its numbers as 10, 2, 4, 6, 12, then dry run (inside the for loop) goes like:

Using Function

This function does the same job as of previous program. But it is created using function. That is, a function arithmeticMean() takes two arguments, the first one is the array, and second is the size. And returns the arithmetic mean of all numbers stored in the array. To lean more about function in C++, you can follow the separate tutorial on it.

// Find Arithmetic Mean using Function
// ----codescracker.com

#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 2 as size, and 10, 3 as two numbers:

arithmetic mean using function c++

Same Program in Other Languages


« Previous Program Next Program »