Largest triangle that can be inscribed in a semicircle - GeeksforGeeks

16 Mar, 2021
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++
Java
Python 3
C#
PHP
Javascript
// C++ Program to find the biggest triangle// which can be inscribed within the semicircle#include <bits/stdc++.h>using namespace std; // Function to find the area// of the trianglefloat trianglearea(float r){     // the radius cannot be negative    if (r < 0)        return -1;     // area of the triangle     return r * r;} // Driver codeint main(){    float r = 5;    cout << trianglearea(r) << endl;    return 0;}
// Java  Program to find the biggest triangle// which can be inscribed within the semicircleimport java.io.*; class GFG {     // Function to find the area// of the trianglestatic float trianglearea(float r){     // the radius cannot be negative    if (r < 0)        return -1;     // area of the triangle    return r * r;} // Driver code      public static void main (String[] args) {        float r = 5;    System.out.println( trianglearea(r));    }}// This code is contributed // by chandan_jnu.
# Python 3 Program  to find the biggest triangle# which can be inscribed within the semicircle # Function to find the area# of the triangledef trianglearea(r) :     # the radius cannot be negative    if r < 0 :        return -1     #  area of the triangle    return r * r  # Driver Codeif __name__ == "__main__" :     r = 5    print(trianglearea(r)) # This code is contributed by ANKITRAI1
// C# Program to find the biggest// triangle which can be inscribed// within the semicircleusing System; class GFG{     // Function to find the area// of the trianglestatic float trianglearea(float r){     // the radius cannot be negative    if (r < 0)        return -1;     // area of the triangle    return r * r;} // Driver codepublic static void Main (){    float r = 5;    Console.Write(trianglearea(r));}} // This code is contributed// by ChitraNayal
<?php// PHP Program to find the biggest// triangle which can be inscribed// within the semicircle // Function to find the area// of the trianglefunction 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?>
<script>  // javascript  Program to find the biggest triangle// which can be inscribed within the semicircle // Function to find the area// of the trianglefunction 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>
25
 
ankthon
Chandan_Kumar
ukasp
inderDuMCA
princi singh
circle
triangle
Geometric
Mathematical
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
Convex Hull using Divide and Conquer Algorithm
Equation of circle when three points on the circle are given
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