Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Harmonic mean using Arithmetic mean and Geometric mean using C++.
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
#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
Advertisements