Given an ellipse, with major axis length **2a** & **2b**. The task is to find the area of the largest rectangle that can be inscribed in it.

**Examples**:

Input: a = 4, b = 3 Output: 24 Input: a = 10, b = 8 Output: 160

**Approach**:

Let the upper right corner of the rectangle has co-ordinates **(x, y)**,

Then the area of rectangle, **A = 4*x*y**.

Now,

Equation of ellipse,

(x^{2}/a^{2}) + (y^{2}/b^{2}) = 1

Thinking of the area as a function ofx, we have

dA/dx = 4xdy/dx + 4yDifferentiating equation of ellipse with respect to x, we have

2x/a,^{2}+ (2y/b^{2})dy/dx = 0so,

dy/dx = -b,^{2}x/a^{2}y

and

dAdx = 4y – (4b^{2}x^{2}/a^{2}y)Setting this to 0 and simplifying, we have

y.^{2}= b^{2}x^{2}/a^{2}From equation of ellipse we know that,

y^{2}=b^{2}– b^{2}x^{2}/a2Thus,

y,^{2}=b^{2}– y^{2}2y, and^{2}=b^{2}y.^{2}b^{2}= 1/2Clearly, then,

xas well, and the area is maximized when^{2}a^{2}= 1/2

x= a/√2andy=b/√2So the maximum area Area,

A_{max}= 2ab

Below is the implementation of the above approach:

## C++

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

*chevron_right*

*filter_none*

## Java

`// Java Program to find the biggest rectangle ` `// which can be inscribed within the ellipse ` ` ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` ` `class` `GFG{ ` `// Function to find the area ` `// of the rectangle ` `static` `float` `rectanglearea(` `float` `a, ` `float` `b) ` `{ ` ` ` ` ` `// a and b cannot be negative ` ` ` `if` `(a < ` `0` `|| b < ` `0` `) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// area of the rectangle ` ` ` `return` `2` `* a * b; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `float` `a = ` `10` `, b = ` `8` `; ` ` ` `System.out.println(rectanglearea(a, b)); ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 Program to find the biggest rectangle ` `# which can be inscribed within the ellipse ` ` ` `# Function to find the area ` `# of the rectangle ` `def` `rectanglearea(a, b) : ` ` ` ` ` `# a and b cannot be negative ` ` ` `if` `a < ` `0` `or` `b < ` `0` `: ` ` ` `return` `-` `1` ` ` ` ` `# area of the rectangle ` ` ` `return` `2` `*` `a ` `*` `b ` ` ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a, b ` `=` `10` `, ` `8` ` ` `print` `(rectanglearea(a, b)) ` ` ` ` ` `# This code is contributed by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find the ` `// biggest rectangle which ` `// can be inscribed within ` `// the ellipse ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function to find the area ` `// of the rectangle ` `static` `float` `rectanglearea(` `float` `a, ` ` ` `float` `b) ` `{ ` ` ` ` ` `// a and b cannot be negative ` ` ` `if` `(a < 0 || b < 0) ` ` ` `return` `-1; ` ` ` ` ` `// area of the rectangle ` ` ` `return` `2 * a * b; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `float` `a = 10, b = 8; ` ` ` `Console.WriteLine(rectanglearea(a, b)); ` `} ` `} ` ` ` `// This code is contributed ` `// by inder_verma ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find the biggest ` `// rectangle which can be inscribed ` `// within the ellipse ` ` ` `// Function to find the area ` `// of the rectangle ` `function` `rectanglearea(` `$a` `, ` `$b` `) ` `{ ` ` ` ` ` `// a and b cannot be negative ` ` ` `if` `(` `$a` `< 0 ` `or` `$b` `< 0) ` ` ` `return` `-1; ` ` ` ` ` `// area of the rectangle ` ` ` `return` `2 * ` `$a` `* ` `$b` `; ` `} ` ` ` `// Driver code ` `$a` `= 10; ` `$b` `= 8; ` `echo` `rectanglearea(` `$a` `, ` `$b` `); ` ` ` `// This code is contributed ` `// by inder_verma ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

160

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 ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle
- Area of a triangle inscribed in a rectangle which is inscribed in an ellipse
- Area of the Largest square that can be inscribed in an ellipse
- Area of the biggest ellipse inscribed within a rectangle
- Area of a circle inscribed in a rectangle which is inscribed in a semicircle
- Area of largest triangle that can be inscribed within a rectangle
- Ratio of area of a rectangle with the rectangle inscribed in it
- Find the area of largest circle inscribed in ellipse
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse
- Largest triangle that can be inscribed in an ellipse
- Area of the biggest possible rhombus that can be inscribed in a rectangle
- Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
- 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
- Radius of the biggest possible circle inscribed in rhombus which in turn is inscribed in a rectangle
- Largest rectangle that can be inscribed in a semicircle
- 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.