# Largest Square that can be inscribed within a hexagon

Given a regular hexagonof side length **a**, the task is to find the area of the largest square that can be inscribed within it.

**Examples:**

Input:a = 6

Output:57.8817

Input:a = 8

Output:102.901

**Approach:** The square we will derive will have the same centre and axes of the hexagon. This is because the square will become smaller if we will rotate it.

The sides of the hexagonal are equal i.e.

a = b + c.

Now, letdbe the length of the side of the inscribed square,

Then the top side of the square,d = 2 * c * sin(60).

And, the left side of the square,d = a + 2 * b * sin(30).

Substituting for c,d = 2 * (a – b) * sin(60).

Now taking d and re-arranging, we get,b / a = (2 * sin(60) – 1) / (2 * (sin(30) + sin(60)))

So,b / a = 2 – √3

Now, substituting the relation ofbandain the left hand side equation of square, we get,

d / a = 3 – √3i.e.d / a = 1.268

Therefore,d = 1.268 * a

Below is the implementation of the above approach:

## C++

`// C++ program to find the area of the largest square ` `// that can be inscribed within the hexagon ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the area ` `// of the square ` `float` `squareArea(` `float` `a) ` `{ ` ` ` ` ` `// Side cannot be negative ` ` ` `if` `(a < 0) ` ` ` `return` `-1; ` ` ` ` ` `// Area of the square ` ` ` `float` `area = ` `pow` `(1.268, 2) * ` `pow` `(a, 2); ` ` ` `return` `area; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `float` `a = 6; ` ` ` `cout << squareArea(a) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the area of the largest square ` `// that can be inscribed within the hexagon ` `class` `Solution { ` ` ` `// Function to find the area ` ` ` `// of the square ` ` ` `static` `float` `squareArea(` `float` `a) ` ` ` `{ ` ` ` ` ` `// Side cannot be negative ` ` ` `if` `(a < ` `0` `) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// Area of the square ` ` ` `float` `area = (` `float` `)(Math.pow(` `1.268` `, ` `2` `) * Math.pow(a, ` `2` `)); ` ` ` `return` `area; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `float` `a = ` `6` `; ` ` ` `System.out.println(squareArea(a)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find the area of the largest square ` `# that can be inscribed within the hexagon ` ` ` `# Function to find the area ` `# of the square ` `def` `squareArea(a): ` ` ` ` ` `# Side cannot be negative ` ` ` `if` `(a < ` `0` `): ` ` ` `return` `-` `1` `; ` ` ` ` ` `# Area of the square ` ` ` `area ` `=` `(` `1.268` `*` `*` `2` `) ` `*` `(a ` `*` `*` `2` `); ` ` ` `return` `area; ` ` ` `# Driver code ` `a ` `=` `6` `; ` `print` `(squareArea(a)); ` ` ` `# This code contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the area of the largest square ` `// that can be inscribed within the hexagon ` `using` `System; ` `class` `Solution { ` ` ` `// Function to find the area ` ` ` `// of the square ` ` ` `static` `float` `squareArea(` `float` `a) ` ` ` `{ ` ` ` ` ` `// Side cannot be negative ` ` ` `if` `(a < 0) ` ` ` `return` `-1; ` ` ` ` ` `// Area of the square ` ` ` `float` `area = (` `float` `)(Math.Pow(1.268, 2) * Math.Pow(a, 2)); ` ` ` `return` `area; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `float` `a = 6; ` ` ` `Console.WriteLine(squareArea(a)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the area of the largest square ` `// that can be inscribed within the hexagon ` ` ` `// Function to find the area ` `// of the square ` `function` `squareArea(` `$a` `) ` `{ ` ` ` ` ` `// Side cannot be negative ` ` ` `if` `(` `$a` `< 0) ` ` ` `return` `-1; ` ` ` ` ` `// Area of the square ` ` ` `$area` `= pow(1.268, 2) * pow(` `$a` `, 2); ` ` ` `return` `$area` `; ` `} ` ` ` `// Driver code ` `$a` `= 6; ` `echo` `squareArea(` `$a` `), ` `"\n"` `; ` ` ` `// This code is contributed by Tushil ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

57.8817

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Largest hexagon that can be inscribed within a square
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon
- Area of a square inscribed in a circle which is inscribed in a hexagon
- Area of the Largest Triangle inscribed in a Hexagon
- Largest hexagon that can be inscribed within an equilateral triangle
- Largest square that can be inscribed in a semicircle
- Area of the Largest square that can be inscribed in an ellipse
- Area of a square inscribed in a circle which is inscribed in an equilateral triangle
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse
- Area of a circle inscribed in a regular hexagon
- Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube
- Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone
- Largest sphere that can be inscribed in a right circular cylinder inscribed in a frustum
- Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within 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.