# Mean

Mean is average of a given set of data. Let us consider below example 2, 4, 4, 4, 5, 5, 7, 9 the mean (average) of a given set of data is 5

**Fact about Mean : **

- The mean (or average) is the most popular and well known measure of central tendency.
- It can be used with both discrete and continuous data, although its use is most often with continuous data.
- There are other types of means.Geometric mean, Harmonic mean and Arithmetic mean.
- Mean is the only measure of central tendency where the sum of the deviations of each value from the mean is always zero.

**Formula of mean of ungrouped data :**

**Formula of mean of grouped data :**

**How to find Mean ?**

Given *n* size unsorted array, find its mean.

Mean of an array =(sum of all elements) / (number of elements)Since the array is not sorted here, we sort the array first, then apply above formula.

**Examples:**

Input :{1, 3, 4, 2, 6, 5, 8, 7}Output :Mean = 4.5 Sum of the elements is 1 + 3 + 4 + 2 + 6 + 5 + 8 + 7 = 36 Mean = 36/8 = 4.5Input :{4, 4, 4, 4, 4}Output :Mean = 4

Below is the code implementation:

## C++

`// CPP program to find mean ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function for calculating mean ` `double` `findMean(` `int` `a[], ` `int` `n) ` `{ ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `sum += a[i]; ` ` ` ` ` `return` `(` `double` `)sum / (` `double` `)n; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `a[] = { 1, 3, 4, 2, 7, 5, 8, 6 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `cout << ` `"Mean = "` `<< findMean(a, n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find mean ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function for calculating mean ` ` ` `public` `static` `double` `findMean(` `int` `a[], ` `int` `n) ` ` ` `{ ` ` ` `int` `sum = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `sum += a[i]; ` ` ` ` ` `return` `(` `double` `)sum / (` `double` `)n; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `a[] = { ` `1` `, ` `3` `, ` `4` `, ` `2` `, ` `7` `, ` `5` `, ` `8` `, ` `6` `}; ` ` ` `int` `n = a.length; ` ` ` `System.out.println(` `"Mean = "` `+ findMean(a, n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find mean ` ` ` `# Function for calculating mean ` `def` `findMean(a, n): ` ` ` ` ` `sum` `=` `0` ` ` `for` `i ` `in` `range` `( ` `0` `, n): ` ` ` `sum` `+` `=` `a[i] ` ` ` ` ` `return` `float` `(` `sum` `/` `n) ` ` ` `# Driver program ` `a ` `=` `[ ` `1` `, ` `3` `, ` `4` `, ` `2` `, ` `7` `, ` `5` `, ` `8` `, ` `6` `] ` `n ` `=` `len` `(a) ` `print` `(` `"Mean ="` `, findMean(a, n)) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find mean ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// Function for ` ` ` `// calculating mean ` ` ` `public` `static` `double` `findMean(` `int` `[] a, ` `int` `n) ` ` ` `{ ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `sum += a[i]; ` ` ` ` ` `return` `(` `double` `)sum / (` `double` `)n; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[] a = { 1, 3, 4, 2, ` ` ` `7, 5, 8, 6 }; ` ` ` `int` `n = a.Length; ` ` ` `Console.Write(` `"Mean = "` `+ findMean(a, n) + ` `"\n"` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find mean ` ` ` `// Function for calculating mean ` `function` `findMean(&` `$a` `, ` `$n` `) ` `{ ` ` ` `$sum` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `$sum` `+= ` `$a` `[` `$i` `]; ` ` ` ` ` `return` `(double)` `$sum` `/ ` ` ` `(double)` `$n` `; ` `} ` ` ` `// Driver Code ` `$a` `= ` `array` `(1, 3, 4, 2, ` ` ` `7, 5, 8, 6); ` `$n` `= sizeof(` `$a` `); ` `echo` `"Mean = "` `. ` ` ` `findMean(` `$a` `, ` `$n` `).` `"\n"` `; ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Mean = 4.5

Time Complexity to find mean = O(n)

**Basic Program related to Mean **

- Find mean of subarray means in a given array
- Mean of array using recursion
- Find the mean vector of a Matrix
- Mean of range in array
- Mean and Median of a matrix

## Recommended Posts:

- Right most non-zero digit in multiplication of array elements
- Find the remainder when First digit of a number is divided by its Last digit
- Find the remaining balance after the transaction
- Count of integers that divide all the elements of the given array
- Count number of ways to get Odd Sum
- Percentage increase in the volume of cuboid if length, breadth and height are increased by fixed percentages
- Count the number of occurrences of a particular digit in a number
- Find number of factors of N when location of its two factors whose product is N is given
- Square free semiprimes in a given range using C++ STL
- Count number of pairs in array having sum divisible by K | SET 2
- Count number of pairs of lines intersecting at a Point
- Maximum number of region in which N non-parallel lines can divide a plane
- 21 Matchsticks Problem
- Minimum Possible sum of digits in a positive multiple of N
- Find the smallest positive number which can not be represented by given digits