# Largest trapezoid that can be inscribed in a semicircle

• Last Updated : 29 Jun, 2022

Given a semicircle of radius r, the task is to find the largest trapezoid that can be inscribed in the semicircle, with base lying on the diameter.
Examples:

```Input: r = 5
Output: 32.476

Input: r = 8
Output: 83.1384``` Approach: Let r be the radius of the semicircle, x be the lower edge of the trapezoid, and y the upper edge, & h be the height of the trapezoid.
Now from the figure,

r^2 = h^2 + (y/2)^2
or, 4r^2 = 4h^2 + y^2
y^2 = 4r^2 – 4h^2
y = 2√(r^2 – h^2)
We know, Area of Trapezoid, A = (x + y)*h/2
So, A = hr + h√(r^2 – h^2)
taking the derivative of this area function with respect to h, (noting that r is a constant since we are given the semicircle of radius r to start with)
dA/dh = r + √(r^2 – h^2) – h^2/√(r^2 – h^2)
To find the critical points we set the derivative equal to zero and solve for h, we get
h = √3/2 * r
So, x = 2 * r & y = r
So, A = (3 * √3 * r^2)/4

Below is the implementation of above approach

## C++

 `// C++ Program to find the biggest trapezoid``// which can be inscribed within the semicircle``#include ``using` `namespace` `std;` `// Function to find the area``// of the biggest trapezoid``float` `trapezoidarea(``float` `r)``{` `    ``// the radius cannot be negative``    ``if` `(r < 0)``        ``return` `-1;` `    ``// area of the trapezoid``    ``float` `a = (3 * ``sqrt``(3) * ``pow``(r, 2)) / 4;` `    ``return` `a;``}` `// Driver code``int` `main()``{``    ``float` `r = 5;``    ``cout << trapezoidarea(r) << endl;``    ``return` `0;``}`

## Java

 `// Java Program to find the biggest trapezoid``// which can be inscribed within the semicircle` `import` `java.util.*;``import` `java.lang.*;``import` `java.io.*;` `class` `GFG{``// Function to find the area``// of the biggest trapezoid``static` `float` `trapezoidarea(``float` `r)``{` `    ``// the radius cannot be negative``    ``if` `(r < ``0``)``        ``return` `-``1``;` `    ``// area of the trapezoid``    ``float` `a = (``3` `* (``float``)Math.sqrt(``3``)``            ``* (``float``)Math.pow(r, ``2``)) / ``4``;` `    ``return` `a;``}` `// Driver code``public` `static` `void` `main(String args[])``{``    ``float` `r = ``5``;``    ``System.out.printf(``"%.3f"``,trapezoidarea(r));``}``}`

## Python 3

 `# Python 3 Program to find the biggest trapezoid``# which can be inscribed within the semicircle` `# from math import everything``from` `math ``import` `*` `# Function to find the area``# of the biggest trapezoid``def` `trapezoidarea(r) :` `    ``# the radius cannot be negative``    ``if` `r < ``0` `:``        ``return` `-``1` `    ``# area of the trapezoid``    ``a ``=` `(``3` `*` `sqrt(``3``) ``*` `pow``(r,``2``)) ``/` `4` `    ``return` `a`  `# Driver code    ``if` `__name__ ``=``=` `"__main__"` `:` `    ``r ``=` `5` `    ``print``(``round``(trapezoidarea(r),``3``))`  `# This code is contributed by ANKITRAI1`

## C#

 `// C# Program to find the biggest``// trapezoid which can be inscribed``// within the semicircle``using` `System;` `class` `GFG``{``// Function to find the area``// of the biggest trapezoid``static` `float` `trapezoidarea(``float` `r)``{` `    ``// the radius cannot be negative``    ``if` `(r < 0)``        ``return` `-1;` `    ``// area of the trapezoid``    ``float` `a = (3 * (``float``)Math.Sqrt(3) *``                   ``(``float``)Math.Pow(r, 2)) / 4;` `    ``return` `a;``}` `// Driver code``public` `static` `void` `Main()``{``    ``float` `r = 5;``    ``Console.WriteLine(``""` `+ trapezoidarea(r));``}``}` `// This code is contributed``// by inder_verma`

## PHP

 ``

## Javascript

 ``

Output:

`32.476`

Time complexity: O(1)

Auxiliary space: O(1)

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.6648425,"math_prob":0.9663734,"size":3952,"snap":"2022-27-2022-33","text_gpt3_token_len":1390,"char_repetition_ratio":0.18161094,"word_repetition_ratio":0.2991014,"special_character_ratio":0.34564778,"punctuation_ratio":0.11963589,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99986374,"pos_list":[0,1,2],"im_url_duplicate_count":[null,5,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-19T20:25:52Z\",\"WARC-Record-ID\":\"<urn:uuid:23c394cd-dd89-4e8e-aee1-4364da46d217>\",\"Content-Length\":\"158989\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:af601b28-a146-4a88-816d-8e1390619488>\",\"WARC-Concurrent-To\":\"<urn:uuid:cf37a343-4f97-4189-8013-5074c79c55c2>\",\"WARC-IP-Address\":\"23.222.4.150\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/largest-trapezoid-that-can-be-inscribed-in-a-semicircle/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:GRE7F2SYKETKUNGNKGBALO44TT3GVNCJ\",\"WARC-Block-Digest\":\"sha1:RD3IN5DBQE46VIIQF47IDFJLRL7O6XT4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882573760.75_warc_CC-MAIN-20220819191655-20220819221655-00362.warc.gz\"}"}