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 = 2 Output: 1.25 Input: a = 5, b= 3 Output: 0.604444

**Approach**: If a square is inscribed in an ellipse, the distance from the centre of the square to any of its corners will be equal to the distance between the origin and the point on the upper right corner in the diagram below, where **x=y**

the equation of the ellipse is

x^2/a^2 + y^2/b^2 = 1

If,x = y

then,x^2/a^2 + x^2/b^2 = 1

therefore,x = √(a^2 + b^2)/ab

so,y = √(a^2 + b^2)/ab

So Area,A = 4(a^2 + b^2)/a^2b^2

Below is the implementation of above approach:

## C++

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

*chevron_right*

*filter_none*

## Java

`// Java Program to find the biggest square ` `// which can be inscribed within the ellipse ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find the area ` `// of the square ` `static` `float` `squarearea(` `float` `a, ` `float` `b) ` `{ ` ` ` ` ` `// a and b cannot be negative ` ` ` `if` `(a < ` `0` `|| b < ` `0` `) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// area of the square ` ` ` `float` `area = ` `4` `*(` `float` `) ((Math.pow(a, ` `2` `) + Math.pow(b, ` `2` `)) ` ` ` `/ (Math.pow(a, ` `2` `) * Math.pow(b, ` `2` `))); ` ` ` ` ` `return` `area; ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `float` `a = ` `4` `, b = ` `2` `; ` ` ` `System.out.println( squarearea(a, b)); ` ` ` ` ` `} ` `} ` `// This code is contributed by inder_verma. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python3 Program to find the biggest square ` `# which can be inscribed within the ellipse ` ` ` ` ` `# Function to find the area ` `# of the square ` `def` `squarearea( a, b): ` ` ` ` ` ` ` `# a and b cannot be negative ` ` ` `if` `(a < ` `0` `or` `b < ` `0` `): ` ` ` `return` `-` `1` ` ` ` ` ` ` `# area of the square ` ` ` `area ` `=` `4` `*` `(((` `pow` `(a, ` `2` `) ` `+` `pow` `(b, ` `2` `)) ` `/` ` ` `(` `pow` `(a, ` `2` `) ` `*` `pow` `(b, ` `2` `)))) ` ` ` ` ` `return` `area ` ` ` ` ` `# Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `a ` `=` `4` ` ` `b ` `=` `2` ` ` `print` `(squarearea(a, b)) ` ` ` `# This code is contributed by ash264 ` |

*chevron_right*

*filter_none*

## C#

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

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find the biggest square ` `// which can be inscribed within the ellipse ` ` ` ` ` `// Function to find the area ` `// of the square ` `function` `squarearea( ` `$a` `, ` `$b` `) ` `{ ` ` ` ` ` `// a and b cannot be negative ` ` ` `if` `(` `$a` `< 0 ` `or` `$b` `< 0) ` ` ` `return` `-1; ` ` ` ` ` ` ` `// area of the square ` ` ` `$area` `= 4 * (((pow(` `$a` `, 2) + pow(` `$b` `, 2)) / ` ` ` `(pow(` `$a` `, 2) * pow(` `$b` `, 2)))); ` ` ` ` ` `return` `$area` `; ` `} ` ` ` `// Driver code ` ` ` ` ` `$a` `= 4; ` ` ` `$b` `= 2; ` ` ` `print` `(squarearea(` `$a` `, ` `$b` `)); ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1.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 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
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse
- Area of Largest rectangle that can be inscribed in an Ellipse
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Find the area of largest circle inscribed in ellipse
- Area of a square inscribed in a circle which is inscribed in an equilateral triangle
- Area of a square inscribed in a circle which is inscribed in a hexagon
- Largest triangle that can be inscribed in an ellipse
- 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 the biggest ellipse inscribed within a rectangle
- Area of a circle inscribed in a rectangle which is inscribed in a semicircle
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon
- Area of largest triangle that can be inscribed within a rectangle
- Area of largest Circle that can be inscribed in a SemiCircle
- Largest square that can be inscribed in a semicircle

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.