Find the length of the median of a Triangle if length of sides are given - GeeksforGeeks

16 Jul, 2021
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 triangle is a line segment joining a vertex to the midpoint of the opposite side, thus bisecting that side.  
Examples:  
Input: a = 8, b = 10, c = 13 Output: 10.89Input: a = 4, b = 3, c = 5 Output: 3.61  
Approach: The idea is to use Apollonius’s theorem to solve this problem. 
Apollonius’s Theorem states 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 Theorem 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++
Java
Python3
C#
Javascript
// 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 trianglefloat median(int a, int b, int c){    float n = sqrt(2 * b * b +                   2 * c * c - a * a) / 2;    return n;} // Driver codeint 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
// Java program to find the length of the// median using sides of the triangleimport java.util.*; class GFG{     // Function to return the length of// the median using sides of trianglepublic 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 codepublic 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
# 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 Codea = 4b = 3c = 5 # Function Callans = median(a, b, c) # Print the final answerprint(round(ans, 2))
// C# program to find the length of the// median using sides of the triangleusing System; class GFG{     // Function to return the length of// the median using sides of trianglepublic 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 codepublic static void Main(String[] args){    int a, b, c;    a = 4;    b = 3;    c = 5;     // Function call    float ans = median(a, b, c);     // Print readonly answer with 2    // digits after decimal    Console.WriteLine(String.Format("{0:F2}", ans));}} // This code is contributed by gauravrajput1
<script> // JavaScript program to find the length of the// median using sides of the triangle // Function to return the length of// the median using sides of trianglefunction median(a, b, c){    let n = (Math.sqrt(2 * b * b +                                2 * c * c -                                a * a) / 2);    return n;}     // Driver Code      let a, b, c;    a = 4;    b = 3;    c = 5;       // Function call    let ans = median(a, b, c);       // Print final answer with 2    // digits after decimal    document.write(ans, 2);           </script>
3.61
 
Time Complexity: O(1) Space Complexity: O(1)  
himanshu77
divyeshrabadiya07
GauravRajput1
splevel62
rajeev0719singh
median-finding
triangle
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convex Hull using Divide and Conquer Algorithm
Equation of circle when three points on the circle are given
Orientation of 3 ordered points
Circle and Lattice Points
Program to find slope of a line
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7