Given a semicircle of radius** r**, we have to find the largest rectangle that can be inscribed in the semicircle, with base lying on the diameter.

**Examples:**

Input : r = 4 Output : 16 Input : r = 5 Output :25

Let **r** be the radius of the semicircle, **x** one half of the base of the rectangle, and **y** the height of the rectangle. We want to maximize the area, A = 2xy.

So from the diagram we have,

**y = √(r^2 – x^2)**

So, **A = 2*x*(√(r^2 – x^2))**, or **dA/dx = 2*√(r^2 – x^2) -2*x^2/√(r^2 – x^2)**

Setting this derivative equal to 0 and solving for x,

**dA/dx = 0 **

or, **2*√(r^2 – x^2) – 2*x^2/√(r^2 – x^2) = 0**

**2r^2 – 4x^2 = 0
x = r/√2**

This is the maximum of the area as,

**dA/dx > 0 **when **x > r/√2**

and, **dA/dx < 0** when **x > r/√2**

Since **y =√(r^2 – x^2)** we then have

**y = r/√2**

Thus, the base of the rectangle has **length = r/√2** and its height has **length √2*r/2**.

So, Area, **A=r^2**

## C++

`// C++ Program to find the ` `// the biggest rectangle ` `// which can be inscribed ` `// within the semicircle ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the area ` `// of the biggest rectangle ` `float` `rectanglearea(` `float` `r) ` `{ ` ` ` ` ` `// the radius cannot be negative ` ` ` `if` `(r < 0) ` ` ` `return` `-1; ` ` ` ` ` `// area of the rectangle ` ` ` `float` `a = r * r; ` ` ` ` ` `return` `a; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `float` `r = 5; ` ` ` `cout << rectanglearea(r) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find the ` `// the biggest rectangle ` `// which can be inscribed ` `// within the semicircle ` `class` `GFG ` `{ ` ` ` `// Function to find the area ` `// of the biggest rectangle ` `static` `float` `rectanglearea(` `float` `r) ` `{ ` ` ` `// the radius cannot be negative ` `if` `(r < ` `0` `) ` ` ` `return` `-` `1` `; ` ` ` `// area of the rectangle ` `float` `a = r * r; ` ` ` `return` `a; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `float` `r = ` `5` `; ` ` ` `System.out.println((` `int` `)rectanglearea(r)); ` `} ` `} ` ` ` `// This code is contributed ` `// by ChitraNayal ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 Program to find the ` `# the biggest rectangle ` `# which can be inscribed ` `# within the semicircle ` ` ` `# Function to find the area ` `# of the biggest rectangle ` `def` `rectanglearea(r) : ` ` ` ` ` `# the radius cannot ` ` ` `# be negative ` ` ` `if` `r < ` `0` `: ` ` ` `return` `-` `1` ` ` ` ` `# area of the rectangle ` ` ` `a ` `=` `r ` `*` `r ` ` ` ` ` `return` `a ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `r ` `=` `5` ` ` ` ` `# function calling ` ` ` `print` `(rectanglearea(r)) ` ` ` `# This code is contributed ` `# by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find the ` `// the biggest rectangle ` `// which can be inscribed ` `// within the semicircle ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the area ` `// of the biggest rectangle ` `static` `float` `rectanglearea(` `float` `r) ` `{ ` ` ` `// the radius cannot be negative ` `if` `(r < 0) ` ` ` `return` `-1; ` ` ` `// area of the rectangle ` `float` `a = r * r; ` ` ` `return` `a; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `float` `r = 5; ` ` ` `Console.Write((` `int` `)rectanglearea(r)); ` `} ` `} ` ` ` `// This code is contributed ` `// by ChitraNayal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find the ` `// the biggest rectangle ` `// which can be inscribed ` `// within the semicircle ` ` ` `// Function to find the area ` `// of the biggest rectangle ` `function` `rectanglearea(` `$r` `) ` `{ ` ` ` ` ` `// the radius cannot ` ` ` `// be negative ` ` ` `if` `(` `$r` `< 0) ` ` ` `return` `-1; ` ` ` ` ` `// area of the rectangle ` ` ` `$a` `= ` `$r` `* ` `$r` `; ` ` ` ` ` `return` `$a` `; ` `} ` ` ` `// Driver code ` `$r` `= 5; ` `echo` `rectanglearea(` `$r` `).` `"\n"` `; ` ` ` `// This code is contributed ` `// by ChitraNayal ` `?> ` |

*chevron_right*

*filter_none*

**OUTPUT :**

25

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Largest triangle that can be inscribed in a semicircle
- Largest square that can be inscribed in a semicircle
- Largest trapezoid that can be inscribed in a semicircle
- Area of largest Circle that can be inscribed in a SemiCircle
- Area of a triangle inscribed in a rectangle which is inscribed in an ellipse
- Radius of the biggest possible circle inscribed in rhombus which in turn is inscribed in a rectangle
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube
- Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube
- Largest sphere that can be inscribed in a right circular cylinder inscribed in a frustum
- Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone
- Area of Largest rectangle that can be inscribed in an Ellipse
- Area of largest triangle that can be inscribed within a rectangle
- Biggest Reuleaux Triangle inscirbed within a square inscribed in a semicircle
- Area of largest semicircle that can be drawn inside a square
- Area of the biggest possible rhombus that can be inscribed in a rectangle
- The biggest possible circle that can be inscribed in a rectangle
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
- Area of a square inscribed in a circle which is inscribed in an equilateral triangle

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.