Area of a Triangle from the given lengths of medians
Last Updated :
06 Apr, 2023
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
#include <bits/stdc++.h>
using namespace std;
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;
}
int main()
{
int a = 9;
int b = 12;
int c = 15;
double ans = Area_of_Triangle(a, b, c);
cout << ans;
}
|
Java
class GFG{
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;
}
public static void main(String[] args)
{
int a = 9 ;
int b = 12 ;
int c = 15 ;
double ans = Area_of_Triangle(a, b, c);
System.out.println(ans);
}
}
|
Python3
import math
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
a = 9
b = 12
c = 15
ans = Area_of_Triangle(a, b, c)
print ( round (ans, 2 ))
|
C#
using System;
class GFG{
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;
}
public static void Main(String[] args)
{
int a = 9;
int b = 12;
int c = 15;
double ans = Area_of_Triangle(a, b, c);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
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;
}
var a = 9;
var b = 12;
var c = 15;
var ans = Area_of_Triangle(a, b, c);
document.write(ans.toFixed(1));
</script>
|
Time Complexity: O(log x)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...