Program for harmonic mean of numbers | CPP program to find harmonic mean of numbers . ; Function that returns harmonic mean . ; Declare sum variables and initialize with zero . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float harmonicMean ( float arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( float ) 1 / arr [ i ] ; return ( float ) n / sum ; } 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 ; }