Given the length of all three sides of a triangle as **a**, **b** and **c**. The task is to calculate the length of the median of the triangle.

A

median of a triangleis a line segment joining a vertex to the midpoint of the opposite side, thus bisecting that side.

**Examples:**

Input:a = 8, b = 10, c = 13Output:10.89Input:a = 4, b = 3, c = 5Output:3.61

**Approach:** The idea is to use Apollonius’s theorem to solve this problem.

Apollonius’s Theoremstates that “the sum of the squares of any two sides of a triangle equals twice the square on half the third side and twice the square on the median bisecting the third side”.

From the above figure, According to Apollonius’s Theorm we have:

where a, b, and c are the length of sides of the triangle

and m is the length of median of the triangle on side 2*a

Therefore, the length of the median of a triangle from the above equation is given by:

Below is the implementation of the above approach:

## C++

`// C++ program to find the length of the ` `// median using sides of the triangle ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the length of ` `// the median using sides of triangle ` `float` `median(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` `float` `n = ` `sqrt` `(2 * b * b + ` ` ` `2 * c * c - a * a) / 2; ` ` ` `return` `n; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a, b, c; ` ` ` `a = 4; ` ` ` `b = 3; ` ` ` `c = 5; ` ` ` ` ` `// Function call ` ` ` `float` `ans = median(a, b, c); ` ` ` ` ` `// Print final answer with 2 ` ` ` `// digits after decimal ` ` ` `cout << fixed << setprecision(2) << ans; ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by himanshu77 ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the length of the ` `// median using sides of the triangle ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to return the length of ` `// the median using sides of triangle ` `public` `static` `float` `median(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` `float` `n = (` `float` `)(Math.sqrt(` `2` `* b * b + ` ` ` `2` `* c * c - ` ` ` `a * a) / ` `2` `); ` ` ` `return` `n; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a, b, c; ` ` ` `a = ` `4` `; ` ` ` `b = ` `3` `; ` ` ` `c = ` `5` `; ` ` ` ` ` `// Function call ` ` ` `float` `ans = median(a, b, c); ` ` ` ` ` `// Print final answer with 2 ` ` ` `// digits after decimal ` ` ` `System.out.println(String.format(` `"%.2f"` `, ans)); ` `} ` `} ` ` ` `// This code is contributed by divyeshrabadiya07 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to Find the ` `# length of the median using sides ` `# of the triangle ` ` ` `import` `math ` ` ` `# Function to return the length of ` `# the median using sides of triangle. ` `def` `median(a, b, c): ` ` ` ` ` `n ` `=` `(` `1` `/` `2` `)` `*` `math.sqrt(` `2` `*` `(b` `*` `*` `2` `) ` ` ` `+` `2` `*` `(c` `*` `*` `2` `) ` ` ` `-` `a` `*` `*` `2` `) ` ` ` ` ` `return` `n ` ` ` `# Driver Code ` `a ` `=` `4` `b ` `=` `3` `c ` `=` `5` ` ` `# Function Call ` `ans ` `=` `median(a, b, c) ` ` ` `# Print the final answer ` `print` `(` `round` `(ans, ` `2` `)) ` |

*chevron_right*

*filter_none*

**Output:**

3.61

**Time Complexity: **O(1) **Space Complexity: **O(1)

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:

- Length of remaining two sides of a Triangle from a given side and its adjacent angles
- Area of Triangle using Side-Angle-Side (length of two sides and the included angle)
- Program to find Length of Bridge using Speed and Length of Train
- Find the type of triangle from the given sides
- Find other two sides of a right angle triangle
- Find other two sides and angles of a right angle triangle
- Find the hypotenuse of a right angled triangle with given two sides
- Find area of triangle if two vectors of two adjacent sides are given
- Find all sides of a right angled triangle from given hypotenuse and area | Set 1
- Partition the array into two odd length groups with minimized absolute difference between their median
- Minimum length of the shortest path of a triangle
- Minimum and maximum possible length of the third side of a triangle
- Number of ways to cut a stick of length N into in even length at most K units long pieces
- Minimize number of cuts required to break N length stick into N unit length sticks
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Length of longest subarray of length at least 2 with maximum GCD
- Check whether triangle is valid or not if sides are given
- Check whether right angled triangle is valid or not for large sides
- Probability of cutting a rope into three pieces such that the sides form a triangle
- Length of the chord the circle if length of the another chord which is equally inclined through the diameter is given

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.