Given two numbers, first calculate arithmetic mean and geometric mean of these two numbers. Using the arithmetic mean and geometric mean so calculated, find the harmonic mean between the two numbers.

Examples:

Input : a = 2 b = 4 Output : 2.666 Input : a = 5 b = 15 Output : 7.500

Arithmetic Mean: Arithmetic Mean ‘AM’ between two numbers a and b is such a number that AM-a = b-AM. Thus, if we are given these two numbers, the arithmetic mean AM = 1/2(a+b)

Geometric Mean: Geometric Mean ‘GM’ between two numbers a and b is such a number that GM/a = b/GM. Thus, if we are given these two numbers, the geometric mean GM = sqrt(a*b)

Harmonic Mean: Harmonic Mean ‘HM’ between two numbers a and b is such a number that 1/HM – 1/a = 1/b – 1/HM. Thus, if we are given these two numbers, the harmonic mean HM = 2ab/a+b

Now, we also know that

## C++

`// C++ implementation of compution of ` `// arithmetic mean, geometric mean ` `// and harmonic mean ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate arithmetic ` `// mean, geometric mean and harmonic mean ` `double` `compute(` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `double` `AM, GM, HM; ` ` ` ` ` `AM = (a + b) / 2; ` ` ` `GM = ` `sqrt` `(a * b); ` ` ` `HM = (GM * GM) / AM; ` ` ` `return` `HM; ` `} ` ` ` `// Driver function ` `int` `main() ` `{ ` ` ` ` ` `int` `a = 5, b = 15; ` ` ` `double` `HM = compute(a, b); ` ` ` `cout << ` `"Harmonic Mean between "` `<< a ` ` ` `<< ` `" and "` `<< b << ` `" is "` `<< HM ; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of compution of ` `// arithmetic mean, geometric mean ` `// and harmonic mean ` `import` `java.io.*; ` ` ` `class` `GeeksforGeeks { ` ` ` ` ` `// Function to calculate arithmetic ` ` ` `// mean, geometric mean and harmonic mean ` ` ` `static` `double` `compute(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` ` ` `double` `AM, GM, HM; ` ` ` ` ` `AM = (a + b) / ` `2` `; ` ` ` `GM = Math.sqrt(a * b); ` ` ` `HM = (GM * GM) / AM; ` ` ` `return` `HM; ` ` ` `} ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `a = ` `5` `, b = ` `15` `; ` ` ` `double` `HM = compute(a, b); ` ` ` `String str = ` `""` `; ` ` ` `str = str + HM; ` ` ` `System.out.print(` `"Harmonic Mean between "` ` ` `+ a + ` `" and "` `+ b + ` `" is "` ` ` `+ str.substring(` `0` `, ` `5` `)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of compution ` `# of arithmetic mean, geometric mean ` `# and harmonic mean ` ` ` `import` `math ` ` ` `# Function to calculate arithmetic ` `# mean, geometric mean and harmonic mean ` `def` `compute( a, b) : ` ` ` `AM ` `=` `(a ` `+` `b) ` `/` `2` ` ` `GM ` `=` `math.sqrt(a ` `*` `b) ` ` ` `HM ` `=` `(GM ` `*` `GM) ` `/` `AM ` ` ` `return` `HM ` ` ` `# Driver function ` `a ` `=` `5` `b ` `=` `15` `HM ` `=` `compute(a, b) ` `print` `(` `"Harmonic Mean between "` `, a, ` ` ` `" and "` `, b , ` `" is "` `, HM ) ` ` ` ` ` `# This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of compution of ` `// arithmetic mean, geometric mean ` `// and harmonic mean ` `using` `System; ` ` ` `class` `GeeksforGeeks { ` ` ` ` ` `// Function to calculate arithmetic ` ` ` `// mean, geometric mean and harmonic mean ` ` ` `static` `double` `compute(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` ` ` `double` `AM, GM, HM; ` ` ` ` ` `AM = (a + b) / 2; ` ` ` `GM = Math.Sqrt(a * b); ` ` ` `HM = (GM * GM) / AM; ` ` ` `return` `HM; ` ` ` `} ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `a = 5, b = 15; ` ` ` `double` `HM = compute(a, b); ` ` ` `Console.WriteLine(` `"Harmonic Mean between "` ` ` `+ a + ` `" and "` `+ b + ` `" is "` ` ` `+HM); ` ` ` `} ` `} ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of compution of ` `// arithmetic mean, geometric mean ` `// and harmonic mean ` ` ` `// Function to calculate arithmetic ` `// mean, geometric mean and harmonic mean ` `function` `compute( ` `$a` `, ` `$b` `) ` `{ ` ` ` ` ` `$AM` `; ` ` ` `$GM` `; ` ` ` `$HM` `; ` ` ` ` ` `$AM` `= (` `$a` `+ ` `$b` `) / 2; ` ` ` `$GM` `= sqrt(` `$a` `* ` `$b` `); ` ` ` `$HM` `= (` `$GM` `* ` `$GM` `) / ` `$AM` `; ` ` ` `return` `$HM` `; ` `} ` ` ` `// Driver Code ` ` ` `$a` `= 5; ` ` ` `$b` `= 15; ` ` ` `$HM` `= compute(` `$a` `, ` `$b` `); ` ` ` `echo` `"Harmonic Mean between "` `.` `$a` `. ` ` ` `" and "` `.` `$b` `. ` `" is "` `.` `$HM` `; ` ` ` `return` `0; ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Harmonic Mean between 5 and 15 is 7.500

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.

## Recommended Posts:

- Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression
- Program for harmonic mean of numbers
- Sum of Arithmetic Geometric Sequence
- Geometric mean (Two Methods)
- Integer part of the geometric mean of the divisors of N
- Program to find the Nth Harmonic Number
- Program to find sum of harmonic series
- Harmonic progression Sum
- Leibniz harmonic triangle
- Harmonic Progression
- Summation of floor of harmonic progression
- Find N Geometric Means between A and B
- Arithmetic Mean
- Number of ways to remove elements to maximize arithmetic mean
- Length of longest sub-array with maximum arithmetic mean.
- Removing a number from array without changing its arithmetic mean
- Sum of N-terms of geometric progression for larger values of N | Set 2 (Using recursion)
- Removing a number from array to make it Geometric Progression
- Program for sum of geometric series
- Number of terms in Geometric Series with given conditions

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.