# Arithmetic Mean in C programming

CServer Side ProgrammingProgramming

Arithmetic mean is the sum of a collection of numbers divided by the number of numbers in the collection.

## Basic properties of Arithmetic Mean

• The mean of n numbers x1, x2, . . ., xn is x. If each observation is increased by p, the mean of the new observations is (x + p).

• The mean of n numbers x1, x2, . . ., xn is x. If each observation is decreased by p, the mean of the new observations is (x - p).

• The mean of numbers x1, x2, . . ., xn is x. If each observation is multiplied by a nonzero number p, the mean of the new observations is px.

• The mean of n numbers x1, x2, . . ., xn is x. If each observation is divided by a nonzero number p, the mean of the new observations is (x/p).

## Formula of Arithmetic Mean

Type 1: Direct mean

Given the array and number of elements

Input - 1,2,3,4,5,6,7,8,9

Output - 5

Explanation - To calculate the arithmetic mean of all 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.

## Example

#include<iostream>
using namespace std;
int main(){
int n, i, sum=0;
int arr[]={1,2,3,4,5,6,7,8,9};
n=9;
for(i=0; i<n; i++) {
sum=sum+arr[i];
}
int armean=sum/n;
cout<<"Arithmetic Mean = "<<armean;
}

Type 2: Range and no of elements present I range is given.

Given three integers X, Y and N. Logic to find N Arithmetic means between X and Y.

N terms in an Arithmetic progression (no. of terms between X and Y)

X= first and
Y= last terms.

Input  - X = 22 Y = 34 N = 5

Output - 24 26 28 30 32

The Arithmetic progression series is

22 24 26 28 30 32 34

Explanation

Let X1, X2, X3, X4……Xn be N Arithmetic Means between two given numbers X and Y.

Then X, X1, X2, X3, X4……Xn, Y will be in Arithmetic Progression. Now Y = (N+2)th term of the Arithmetic progression.

Finding the (N+2)th term of the Arithmetic progression Series, where d is the Common Difference

Y = X + (N + 2 - 1)d
Y - X = (N + 1)d

So the Common Difference d is given by.

d = (Y - X) / (N + 1)

We have the value of A and the value of the common difference(d), now we can find all the N Arithmetic Means between X and Y.

## Example

#include<stdio.h>
int main() {
int X = 22, Y = 34, N = 5;
float d = (float)(Y - X) / (N + 1);
for (int i = 1; i <= N; i++) {
printf("%3f ", (X + i * d));
}
return 0;
}

## Output

24.000000 26.000000 28.000000 30.000000 32.000000
Published on 09-Aug-2019 16:45:22