# 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:

• Find Arithmetic Mean without using Function
• Find Arithmetic Mean using user-defined Function

### 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.

```#include<iostream>
using namespace std;
int main()
{
int n, i;
float arr, 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:

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:

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:

• Initially 0 gets initialized to i. So i=0
• Checks the condition, whether i (0) is less than n (5) or not
• The condition evaluates to be true, therefore program flow goes inside the loop and receives a number from user and stores it in arr[i] (that is arr)
• And the statement sum = sum+arr[i] or sum = 0+arr or sum = 0+10 (supposing user enters 10 as first number). Therefore, sum=10
• Now program flow goes to updatation part of for loop and increments the value of i. Now i=1
• Checks the condition, because condition again evaluates to be true, therefore program flow again goes inside the loop and evaluates two statements
• That is, receives a number say 2 and stores it in arr
• And sum+arr or 10+2 gets initialized to sum. Now sum=12
• Continue the process until the value of i becomes 5 or the condition i<n evaluates to be false
• After exiting from the loop, we'll have a variable sum that holds the sum of all the 5 numbers
• Now apply the final formula of arithmetic mean, that is sum/size, that is sum/5. Put the value of sum. The variable armean holds its value (sum/size). Therefore just print the value of armean as output, that will be the arithmetic mean

### 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.

```#include<iostream>
using namespace std;
float arithmeticMean(float [], int);
int main()
{
int n, i;
float arr, 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:

#### Same Program in Other Languages

C++ Online Test

« Previous Program Next Program »