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.0
Input: 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
// 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 medians 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) * sqrt (x); Â
    return area; } Â
// Driver Code int 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
// Java program to calculate // area of a triangle from the // given lengths of medians class GFG{ Â
// Function to return the area of // triangle using medians static 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 Code public 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
# Python3 program to calculate # area of a triangle from the # given lengths of medians import math Â
# Function to return the area of # triangle using medians def 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 Code a = 9 b = 12 c = 15 Â
# Function Call ans = Area_of_Triangle(a, b, c) Â
# Print the final answer print ( round (ans, 2 )) |
C#
// C# program to calculate // area of a triangle from the // given lengths of medians using System; Â
class GFG{ Â
// Function to return the area of // triangle using medians static 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 Code public 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 |
Javascript
<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(log x)Â
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!