# Program for harmonic mean of numbers

Last Updated : 17 Feb, 2023

Given an array of elements, find harmonic mean of numbers.
Examples:

Input : arr[] = {2.0, 1.0}
Output : 1.3333
Harmonic mean = 2/(1/2.0 + 1/1.0)
= (2 * 2)/3
= 1.333

Input : arr[] = {13.5, 14.5, 14.8, 15.2, 16.1}
Output : 14.7707

Harmonic mean is used when average of rates is required, below is the formula.
Harmonic mean of n numbers x1, x2
x3, . . ., xn can written as below.
Harmonic mean = n / ((1/x1) + (1/x2) + (1/x3) + . . . + (1/xn))

Below is the implementation of Harmonic Mean.

## C++

 // CPP program to find harmonic mean of numbers. #include using namespace std;    // Function that returns harmonic mean. float harmonicMean(float arr[], int n) {     // Declare sum variables and initialize with zero.     float sum = 0;     for (int i = 0; i < n; i++)         sum = sum + (float)1 / arr[i];     return (float)n / sum; }    // Driver code int main() {     float arr[] = { 13.5, 14.5, 14.8, 15.2, 16.1 };     int n = sizeof(arr) / sizeof(arr[0]);     cout << harmonicMean(arr, n);     return 0; }    // This code is contributed by Aditya Kumar (adityakumar129)

## C

 // C program to find harmonic mean of numbers. #include    // Function that returns harmonic mean. float harmonicMean(float arr[], int n) {     // Declare sum variables and initialize with zero.     float sum = 0;     for (int i = 0; i < n; i++)         sum = sum + (float)1 / arr[i];     return (float)n / sum; }    // Driver code int main() {     float arr[] = { 13.5, 14.5, 14.8, 15.2, 16.1 };     int n = sizeof(arr) / sizeof(arr[0]);     printf("%f",harmonicMean(arr, n));     return 0; }    // This code is contributed by Aditya Kumar (adityakumar129)

## Java

 // JAVA program to find harmonic mean of numbers. import java.io.*;    class GFG {        // Function that returns harmonic mean.     static float harmonicMean(float arr[], int n)     {         // Declare sum variables and initialize with zero         float sum = 0;         for (int i = 0; i < n; i++)             sum = sum + (float)1 / arr[i];         return (float)n / sum;     }        // Driver code     public static void main(String args[])     {         float arr[] = { 13.5f, 14.5f, 14.8f, 15.2f, 16.1f };         int n = arr.length;         System.out.println(harmonicMean(arr, n));     } }    // This code is contributed by Aditya Kumar (adityakumar129)

## Python3

 # Python 3 program to find harmonic # mean of numbers.    # Function that returns harmonic mean. def harmonicMean(arr, n) :          # Declare sum variables and      # initialize with zero.     sm = 0     for i in range(0, n) :         sm = sm + (1) / arr[i];         return n / sm       # Driver code arr = [ 13.5, 14.5, 14.8, 15.2, 16.1 ]; n = len(arr) print(harmonicMean(arr, n))    # This code is contributed by Nikita Tiwari.

## C#

 // C# program to find harmonic // mean of numbers. using System;    class GFG {        // Function that returns harmonic mean.     static float harmonicMean(float[] arr, int n)     {         // Declare sum variables and         // initialize with zero         float sum = 0;         for (int i = 0; i < n; i++)             sum = sum + (float)1 / arr[i];            return (float)n / sum;     }        // Driver code     public static void Main()     {         float[] arr = { 13.5f, 14.5f, 14.8f,                         15.2f, 16.1f };         int n = arr.Length;         Console.WriteLine(harmonicMean(arr, n));     } }    /*This code is contributed by vt_m.*/



## Javascript



Output:

14.7707

Time Complexity: O(n)

Auxiliary Space: O(1), since no extra space has been taken.
What if we are given elements and their frequencies?
If we are given n numbers and every number has some frequency then simply we use formula
Harmonic mean = (Frequency-Sum) /((f1/x1) + (f2/x2) + (f3/x3) + . . . + (fn/xn))
Where f1, f2, f3, . . ., fn are the frequencies of elements and x1, 2, x3, . . ., xn are the elements of array.
Frequency-Sum = f1 + f2 + f3, . . ., fn
Examples:

Input : num[] = {13, 14, 15, 16, 17}
freq[] = {2, 5, 13, 7, 3}
Output : 15.0631

## C++

 // C++ program to find harmonic mean. #include using namespace std; // Function that returns harmonic mean. float harmonicMean(int arr[], int freq[], int n) {     float sum = 0, frequency_sum = 0;     for (int i = 0; i < n; i++) {         sum = sum + (float)freq[i] / arr[i];         frequency_sum  = frequency_sum  + freq[i];     }     return frequency_sum / sum; }    // Driver code int main() {     int num[] = { 13, 14, 15, 16, 17 };     int freq[] = { 2, 5, 13, 7, 3 };     int n = sizeof(num) / sizeof(num[0]);     cout << harmonicMean(num, freq, n);     return 0; }

## Java

 // JAVA program to find harmonic mean.    class GFG {            // Function that returns harmonic mean.     static float harmonicMean(int arr[], int freq[],                               int n)     {         float sum = 0, frequency_sum = 0;         for (int i = 0; i < n; i++) {             sum = sum + (float)freq[i] / arr[i];             frequency_sum  = frequency_sum  + freq[i];         }         return (frequency_sum / sum);     }             // Driver code     public static void main(String args[])     {         int num[] = { 13, 14, 15, 16, 17 };         int freq[] = { 2, 5, 13, 7, 3 };         int n = num.length;         System.out.println(harmonicMean(num, freq, n));     } }    /*This code is contributed by Nikita Tiwari.*/

## C#

 // C# program to find harmonic mean. using System;    class GFG {        // Function that returns harmonic mean.     static float harmonicMean(int[] arr, int[] freq,                                               int n)     {         float sum = 0, frequency_sum = 0;         for (int i = 0; i < n; i++) {             sum = sum + (float)freq[i] / arr[i];             frequency_sum = frequency_sum + freq[i];         }         return (frequency_sum / sum);     }        // Driver code     public static void Main()     {         int[] num = { 13, 14, 15, 16, 17 };         int[] freq = { 2, 5, 13, 7, 3 };         int n = num.Length;         Console.WriteLine(harmonicMean(num, freq, n));     } }    /*This code is contributed by vt_m.*/



## Javascript



Output:

15.0631

Time Complexity: O(n)

Auxiliary Space: O(1)
Harmonic mean of numbers using harmonic_mean() in Python:
Simple Python program to find harmonic mean using harmonic_mean() function.

## Python3

 #'harmonic_mean()' new function added in 'Python3.6' onwards. #Program calculates Harmonic Mean using harmonic_mean()    #imports Python statistics library import statistics def harmonic_mean():     list= [13.5, 14.5, 14.8, 15.2, 16.1]     print(statistics.harmonic_mean(list))    #Driver code harmonic_mean()       #This code is contributed by 'Abhishek Agrawal'.

Output: 14.770680729373778

Time Complexity: O(n)

Auxiliary Space: O(1)

Previous
Next