Given a regular hexagon of side length **a**, the task is to find the length of it’s diagonal.

**Examples**:

Input : a = 4 Output : 8 Input : a = 7 Output : 14

From the diagram, it is clear that the triangle ABC is an equilateral triangle, so

**AB = AC = BC = a**.

also it is obvious, diagonal = **2*AC** or **2*BC**

So the length of diagonal of the hexagon = **2*a**

Below is the implementation of the above approach:

## C++

`// C++ Program to find the diagonal ` `// of the hexagon ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the diagonal ` `// of the hexagon ` `float` `hexadiagonal(` `float` `a) ` `{ ` ` ` `// side cannot be negative ` ` ` `if` `(a < 0) ` ` ` `return` `-1; ` ` ` ` ` `// diagonal of the hexagon ` ` ` `return` `2 * a; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `float` `a = 4; ` ` ` ` ` `cout << hexadiagonal(a) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find the diagonal ` `// of the hexagon ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function to find the diagonal ` `// of the hexagon ` `static` `float` `hexadiagonal(` `float` `a) ` `{ ` ` ` `// side cannot be negative ` ` ` `if` `(a < ` `0` `) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// diagonal of the hexagon ` ` ` `return` `2` `* a; ` `} ` ` ` `// Driver code ` ` ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `float` `a = ` `4` `; ` ` ` ` ` `System.out.print( hexadiagonal(a)); ` `} ` `} ` ` ` `// This code is contributed ` `// by shs ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to find the diagonal ` `# of the hexagon ` ` ` `# Function to find the diagonal ` `# of the hexagon ` `def` `hexadiagonal(a): ` ` ` ` ` `# side cannot be negative ` ` ` `if` `(a < ` `0` `): ` ` ` `return` `-` `1` ` ` ` ` `# diagonal of the hexagon ` ` ` `return` `2` `*` `a ` ` ` ` ` `# Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `a ` `=` `4` ` ` `print` `(hexadiagonal(a)) ` ` ` `# This code is contributed by ` `# Shivi_Aggarwal ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find the diagonal ` `// of the hexagon ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find the diagonal ` ` ` `// of the hexagon ` ` ` `static` `float` `hexadiagonal(` `float` `a) ` ` ` `{ ` ` ` `// side cannot be negative ` ` ` `if` `(a < 0) ` ` ` `return` `-1; ` ` ` ` ` `// diagonal of the hexagon ` ` ` `return` `2 * a; ` ` ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `float` `a = 4; ` ` ` `Console.WriteLine( hexadiagonal(a)); ` `} ` `} ` ` ` `// This code is contributed ` `// by anuj_67.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find the diagonal ` `// of the hexagon ` ` ` `// Function to find the diagonal ` `// of the hexagon ` `function` `hexadiagonal(` `$a` `) ` `{ ` ` ` `// side cannot be negative ` ` ` `if` `(` `$a` `< 0) ` ` ` `return` `-1; ` ` ` ` ` `// diagonal of the hexagon ` ` ` `return` `2 * ` `$a` `; ` `} ` ` ` `// Driver code ` ` ` `$a` `= 4; ` ` ` ` ` `echo` `hexadiagonal(` `$a` `); ` ` ` `// This code is contributed ` `// by anuj_67.. ` ` ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

8

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:

- Area of hexagon with given diagonal length
- Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal
- Diagonal of a Regular Hexagon
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Count of Equilateral Triangles of unit length possible from a given Hexagon
- Area of a Hexagon
- Area of the Largest Triangle inscribed in a Hexagon
- Area of a circle inscribed in a regular hexagon
- Area of a square inscribed in a circle which is inscribed in a hexagon
- Largest Square that can be inscribed within a hexagon
- Largest hexagon that can be inscribed within a square
- Largest hexagon that can be inscribed within an equilateral triangle
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon
- Number of coloured 0's in an N-level hexagon
- Count of 0s in an N-level hexagon
- Area of a square from diagonal length
- Length of the Diagonal of the Octagon
- Length of Diagonal of a n-sided regular polygon
- Length of diagonal of a parallelogram using adjacent sides and angle between them

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.