Find Harmonic mean using Arithmetic mean and Geometric mean using C++.

C++Server Side ProgrammingProgramming

Here we will see how to get the Harmonic mean using the arithmetic mean and the geometric mean. The formula for these three means are like below −

  • Arithmetic Mean − (a + b)/2
  • Geometric Mean − $$\sqrt{\lgroup a*b\rgroup}$$
  • Harmonic Mean − 2ab/(a+b)

The Harmonic Mean can be expressed using arithmetic mean and geometric mean using this formula −

$$HM=\frac{GM^{2}}{AM}$$

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
double getHarmonicMean(int a, int b) {
   double AM, GM, HM;
   AM = (a + b) / 2;
   GM = sqrt(a * b);
   HM = (GM * GM) / AM;
   return HM;
}
int main() {
   int a = 5, b = 15;
   double res = getHarmonicMean(a, b);
   cout << "Harmonic Mean of " << a << " and " << b << " is " << res ;
}

Output

Harmonic Mean of 5 and 15 is 7.5
raja
Published on 30-Oct-2019 05:44:56
Advertisements