Area of a Triangle from the given lengths of medians - GeeksforGeeks

01 Feb, 2022
Given three integers A,B and C which denotes length of the three medians of a triangle, the task is to calculate the area 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 = 9, B = 12, C = 15 Output: 72.0Input: A = 39, B = 42, C = 45 Output: 1008.0
Approach: The area of the triangle can be calculated from the given length of medians using the following equation:
 
where 
 
Below is the implementation of the above approach:
C++14
Java
Python3
C#
Javascript
// C++14 program to calculate// area of a triangle from the// given lengths of medians#include <bits/stdc++.h>using namespace std; // Function to return the area of// triangle using mediansdouble Area_of_Triangle(int a, int b, int c){    int s = (a + b + c) / 2;    int x = s * (s - a);    x = x * (s - b);    x = x * (s - c);    double area = (4 / (double)3) * sqrt(x);     return area;} // Driver Codeint main(){    int a = 9;    int b = 12;    int c = 15;         // Function call    double ans = Area_of_Triangle(a, b, c);         // Print the final answer    cout << ans;} // This code is contributed by code_hunt
// Java program to calculate// area of a triangle from the// given lengths of mediansclass GFG{ // Function to return the area of// triangle using mediansstatic double Area_of_Triangle(int a,                               int b, int c){    int s = (a + b + c)/2;    int x = s * (s - a);    x = x * (s - b);    x = x * (s - c);    double area = (4 / (double)3) * Math.sqrt(x);     return area;} // Driver Codepublic static void main(String[] args){    int a = 9;    int b = 12;    int c = 15;         // Function Call    double ans = Area_of_Triangle(a, b, c);         // Print the final answer    System.out.println(ans);}} // This code is contributed by sapnasingh4991
# Python3 program to calculate# area of a triangle from the# given lengths of mediansimport math # Function to return the area of# triangle using mediansdef Area_of_Triangle(a, b, c):     s = (a + b + c)//2    x = s * (s - a)    x = x * (s - b)    x = x * (s - c)    area = (4 / 3) * math.sqrt(x)     return area # Driver Codea = 9b = 12c = 15 # Function Callans = Area_of_Triangle(a, b, c) # Print the final answerprint(round(ans, 2))
// C# program to calculate// area of a triangle from the// given lengths of mediansusing System; class GFG{ // Function to return the area of// triangle using mediansstatic double Area_of_Triangle(int a,                               int b, int c){    int s = (a + b + c) / 2;    int x = s * (s - a);         x = x * (s - b);    x = x * (s - c);         double area = (4 / (double)3) * Math.Sqrt(x);     return area;} // Driver Codepublic static void Main(String[] args){    int a = 9;    int b = 12;    int c = 15;         // Function call    double ans = Area_of_Triangle(a, b, c);         // Print the final answer    Console.WriteLine(ans);}} // This code is contributed by sapnasingh4991
<script>// javascript program to calculate// area of a triangle from the// given lengths of medians     // Function to return the area of    // triangle using medians    function Area_of_Triangle(a , b , c) {        var s = (a + b + c) / 2;        var x = s * (s - a);        x = x * (s - b);        x = x * (s - c);        var area = (4 / 3) * Math.sqrt(x);         return area;    }     // Driver Code         var a = 9;    var b = 12;    var c = 15;     // Function Call    var ans = Area_of_Triangle(a, b, c);     // Print the final answer    document.write(ans.toFixed(1)); // This code is contributed by Rajput-Ji</script>
72.0
Time Complexity: O(1) Auxiliary Space: O(1) 
sapnasingh4991
code_hunt
Rajput-Ji
saurabh1990aror
area-volume-programs
median-finding
triangle
Geometric
Mathematical
School Programming
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Circle and Lattice Points
Queries on count of points lie inside a circle
Equation of circle when three points on the circle are given
Convex Hull using Divide and Conquer Algorithm
Maximum number of region in which N non-parallel lines can divide a plane
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