# Largest hexagon that can be inscribed within a square

Given side of a square **a**, the task is to find the side of the largest hexagon that can be inscribed within the given square.

**Examples:**

Input:a = 6

Output:3.1056

Input:a = 8

Output:4.1408

**Approach:**: Let, the side of the hexagon be **x** and assume that the side of the square, **a** gets divided into smaller length **b** & bigger length **c** i.e. **a = b + c**

Now from the figure, we see,

bwhich gives^{2}+ b^{2}= x^{2}b = x / √2

Now again,d / (2 * x) = cos(30) = √3 / 2i.e.d = x√3

And,cwhich gives^{2}+ c^{2}= d^{2}c = d / √2 = x√3 / √2

Since,a = b + c. So,a = x / √2 + x√3 / √2 = ((1 + √3) / √2) * x = 1.932 * x

So, side of the hexagon,x = 0.5176 * a

Below is the implementation of the above approach:

## C++

`// C++ Program to find the biggest hexagon which ` `// can be inscribed within the given square ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the side ` `// of the hexagon ` `float` `hexagonside(` `float` `a) ` `{ ` ` ` ` ` `// Side cannot be negative ` ` ` `if` `(a < 0) ` ` ` `return` `-1; ` ` ` ` ` `// Side of the hexagon ` ` ` `float` `x = 0.5176 * a; ` ` ` `return` `x; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `float` `a = 6; ` ` ` `cout << hexagonside(a) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find the biggest hexagon which ` `// can be inscribed within the given square ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function to return the side ` `// of the hexagon ` `static` `double` `hexagonside(` `double` `a) ` `{ ` ` ` ` ` `// Side cannot be negative ` ` ` `if` `(a < ` `0` `) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// Side of the hexagon ` ` ` `double` `x = (` `0.5176` `* a); ` ` ` `return` `x; ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `double` `a = ` `6` `; ` ` ` `System.out.println (hexagonside(a)); ` ` ` `} ` `//This code is contributed by ajit. ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 Program to find the biggest ` `# hexagon which can be inscribed within ` `# the given square ` ` ` `# Function to return the side ` `# of the hexagon ` `def` `hexagonside(a): ` ` ` ` ` `# Side cannot be negative ` ` ` `if` `(a < ` `0` `): ` ` ` `return` `-` `1` `; ` ` ` ` ` `# Side of the hexagon ` ` ` `x ` `=` `0.5176` `*` `a; ` ` ` `return` `x; ` ` ` `# Driver code ` `a ` `=` `6` `; ` `print` `(hexagonside(a)); ` ` ` `# This code is contributed ` `# by Akanksha Rai ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find the biggest hexagon which ` `// can be inscribed within the given square ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the side ` `// of the hexagon ` `static` `double` `hexagonside(` `double` `a) ` `{ ` ` ` ` ` `// Side cannot be negative ` ` ` `if` `(a < 0) ` ` ` `return` `-1; ` ` ` ` ` `// Side of the hexagon ` ` ` `double` `x = (0.5176 * a); ` ` ` `return` `x; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `double` `a = 6; ` ` ` `Console.WriteLine(hexagonside(a)); ` `} ` `} ` ` ` `// This code is contributed by Ryuga. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find the biggest hexagon which ` `// can be inscribed within the given square ` ` ` `// Function to return the side of the hexagon ` `function` `hexagonside(` `$a` `) ` `{ ` ` ` ` ` `// Side cannot be negative ` ` ` `if` `(` `$a` `< 0) ` ` ` `return` `-1; ` ` ` ` ` `// Side of the hexagon ` ` ` `$x` `= 0.5176 * ` `$a` `; ` ` ` `return` `$x` `; ` `} ` ` ` `// Driver code ` `$a` `= 6; ` `echo` `hexagonside(` `$a` `); ` ` ` `// This code is contributed by akt_mit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3.1056

## Recommended Posts:

- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Largest Square that can be inscribed within a hexagon
- 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.