Horje
Largest triangle that can be inscribed in a semicircle

Given a semicircle with radius r, we have to find the largest triangle that can be inscribed in the semicircle, with base lying on the diameter.
Examples: 
 

Input: r = 5
Output: 25

Input: r = 8
Output: 64

 

 

Approach: From the figure, we can clearly understand the biggest triangle that can be inscribed in the semicircle has height r. Also, we know the base has length 2r. So the triangle is an isosceles triangle.
 

So, Area A: = (base * height)/2 = (2r * r)/2 = r^2

Below is the implementation of above approach
 

C++

<?php
// PHP Program to find the biggest
// triangle which can be inscribed
// within the semicircle
 
// Function to find the area
// of the triangle
function trianglearea($r)
{
 
    // the radius cannot be negative
    if ($r < 0)
        return -1;
 
    // area of the triangle
    return $r * $r;
}
 
// Driver code
$r = 5;
echo trianglearea($r);
 
// This code is contributed
// by inder_verma
?>

Javascript

<script>
  
// javascript  Program to find the biggest triangle
// which can be inscribed within the semicircle
 
// Function to find the area
// of the triangle
function trianglearea(r)
{
 
    // the radius cannot be negative
    if (r < 0)
        return -1;
 
    // area of the triangle
    return r * r;
}
 
// Driver code
 
var r = 5;
document.write( trianglearea(r));
 
// This code contributed by Princi Singh
 
</script>

Output: 

25

 

Time complexity: O(1) as it is performing constant operations

Auxiliary Space: O(1)




Reffered: https://www.geeksforgeeks.org


Mathematical

Related
Area of Largest rectangle that can be inscribed in an Ellipse Area of Largest rectangle that can be inscribed in an Ellipse
Find the sum of series 3, 7, 13, 21, 31.... Find the sum of series 3, 7, 13, 21, 31....
Find the sum of the series 2, 5, 13, 35, 97... Find the sum of the series 2, 5, 13, 35, 97...
Find sum of the series 1-2+3-4+5-6+7....... Find sum of the series 1-2+3-4+5-6+7.......
Find the sum of n terms of the series 1,8,27,64 .... Find the sum of n terms of the series 1,8,27,64 ....

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
8