Skip to content
Related Articles

Related Articles

Improve Article
Program for harmonic mean of numbers
  • Last Updated : 14 Apr, 2021

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 <bits/stdc++.h>
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;
}

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 Nikita Tiwari.*/

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.*/

PHP




<?php
// PHP program to find
// harmonic mean of numbers.
 
// Function that returns
// harmonic mean.
function harmonicMean($arr, $n)
{
     
    // Declare sum variables and
    // initialize with zero
    $sum = 0;
    for ($i = 0; $i < $n; $i++)
        $sum = $sum + (float)
               (1 / $arr[$i]);
 
    return (float)($n / $sum);
}
 
// Driver code
$arr = array(13.5, 14.5, 14.8, 15.2, 16.1);
$n = sizeof($arr);
echo(harmonicMean($arr, $n));
 
// This code is contributed by Ajit.
?>

Javascript




<script>
 
// Javascript program to find harmonic mean of numbers.
 
// Function that returns harmonic mean.
function harmonicMean(arr, n)
{
    // Declare sum variables and initialize
    // with zero.
    let sum = 0;
    for (let i = 0; i < n; i++)
        sum = sum + (1 / arr[i]);
 
    return n/sum;
}
 
// Driver code
 
let arr = [ 13.5, 14.5, 14.8, 15.2, 16.1 ];
let n = arr.length;
document.write(harmonicMean(arr, n));
 
</script>

Output: 



14.7707

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 <bits/stdc++.h>
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.*/

Python3




# Python 3 program to find harmonic mean.
 
# Function that returns harmonic mean.
def harmonicMean(arr, freq, n) :
    sm = 0
    frequency_sum = 0
    for i in range(0,n) :
        sm = sm + freq[i] / arr[i]
        frequency_sum  = frequency_sum  + freq[i]
     
    return (round(frequency_sum / sm,4))
 
 
# Driver code
num = [ 13, 14, 15, 16, 17 ]
freq = [ 2, 5, 13, 7, 3 ]
n = len(num)
print (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.*/

PHP




<?php
// PHP program to find
// harmonic mean.
 
// Function that returns
// harmonic mean.
function harmonicMean($arr, $freq, $n)
{
    $sum = 0; $frequency_sum = 0;
    for ($i = 0; $i < $n; $i++)
    {
        $sum = $sum + (float)
               ($freq[$i] / $arr[$i]);
        $frequency_sum = $frequency_sum +
                                $freq[$i];
    }
    return ($frequency_sum / $sum);
}
 
// Driver code
$num = array(13, 14, 15, 16, 17);
$freq = array(2, 5, 13, 7, 3);
$n = sizeof($num);
echo(harmonicMean($num, $freq, $n));
 
// This code is contributed by Ajit.
?>

Javascript




<script>
 
// Javascript program to find harmonic mean.
 
// Function that returns harmonic mean.
function harmonicMean(arr, freq, n)
{
    let sum = 0, frequency_sum = 0;
    for (let i = 0; i < n; i++) {
        sum = sum + (freq[i] / arr[i]);
        frequency_sum  = frequency_sum  + freq[i];
    }
    return frequency_sum / sum;
}
 
// Driver code
let num = [ 13, 14, 15, 16, 17 ];
let freq = [ 2, 5, 13, 7, 3 ];
let n = num.length;
document.write(harmonicMean(num, freq, n));
 
</script>

Output: 

15.0631

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

This article is contributed by Dharmendra Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes




My Personal Notes arrow_drop_up
Recommended Articles
Page :