Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle

Given here is a semicircle of radius **r**, which inscribes a rectangle which in turn inscribes an ellipse. The task is to find the area of this largest ellipse.

**Examples:**

Input:r = 5Output:19.625Input:r = 11Output:94.985

**Approach**:

- Let the, length of the rectangle =
**l**and breadth of the rectangle =**b** - Let, the length of the major axis of the ellipse =
**2x**and, the length of the minor axis of the ellipse =**2y** - As we know, length and breadth of the largest rectangle inside a semicircle are
**r/√2**and**√2r**(Please refer here) - Also, Area of the ellipse within the rectangle =
**(π*l*b)/4**=**(πr^2/4)**

**Below is the implementation of above approach**:

## C++

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

*chevron_right*

*filter_none*

## Java

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

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to find the biggest ellipse ` `# which can be inscribed within a rectangle ` `# which in turn is inscribed within a semicircle ` ` ` `# Function to find the area of ` `# the biggest ellipse ` `def` `ellipsearea(r) : ` ` ` ` ` `# the radius cannot be negative ` ` ` `if` `(r < ` `0` `) : ` ` ` `return` `-` `1` `; ` ` ` ` ` `# area of the ellipse ` ` ` `a ` `=` `(` `3.14` `*` `r ` `*` `r) ` `/` `4` `; ` ` ` ` ` `return` `a; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `r ` `=` `5` `; ` ` ` `print` `(ellipsearea(r)); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

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

*chevron_right*

*filter_none*

## PHP

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

*chevron_right*

*filter_none*

**Output:**

19.625

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.