Diagonal of a Regular Hexagon - GeeksforGeeks

17 Mar, 2021
Given an integer a which is the side of a regular hexagon, the task is to find and print the length of its diagonal.Examples:  
Input: a = 6 Output: 10.38Input: a = 9 Output: 15.57  
 
 
Approach: We know that the sum of interior angles of a polygon = (n – 2) * 180 where, n is the number of sides of the polygon. So, sum of interior angles of a hexagon = 4 * 180 = 720 and each interior angle will be 120. Now, we have to find BC = 2 * x. If we draw a perpendicular AO on BC, we will see that the perpendicular bisects BC in BO and OC, as triangles AOB and AOC are congruent to each other. So, in triangle AOB, sin(60) = x / a i.e. x = 0.866 * a Therefore, diagonal length will be 2 * x i.e. 1.73 * a.Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to find the diagonal// of a regular hexagon#include <bits/stdc++.h>using namespace std; // Function to find the diagonal// of a regular hexagonfloat hexDiagonal(float a){     // Side cannot be negative    if (a < 0)        return -1;     // Length of the diagonal    float d = 1.73 * a;    return d;} // Driver codeint main(){    float a = 9;    cout << hexDiagonal(a) << endl;    return 0;}
// Java Program to find the diagonal// of a regular hexagon public class GFG{     // Function to find the diagonal    // of a regular hexagon    static double hexDiagonal(float a)    {             // Side cannot be negative        if (a < 0)            return -1;             // Length of the diagonal        double d = (double)1.73 * a;        return d;    }         // Driver code    public static void main(String []args)    {        float a = 9;        System.out.println(hexDiagonal(a)) ;    }    // This code is contributed by Ryuga}
# Python3 Program to find the diagonal# of a regular hexagon # Function to find the diagonal# of a regular hexagondef hexDiagonal(a):     # Side cannot be negative    if (a < 0):        return -1;     # Length of the diagonal    d = 1.73 * a;    return d;  # Driver codea = 9;print(hexDiagonal(a)); # This code is contributed# by Akanksha Rai
// C# Program to find the diagonal // of a regular hexagonusing System ;public class GFG{       // Function to find the diagonal     // of a regular hexagon     static double hexDiagonal(float a)     {                // Side cannot be negative         if (a < 0)             return -1;                // Length of the diagonal         double d = (double)1.73 * a;         return d;     }            // Driver code     public static void Main()    {         float a = 9;         Console.WriteLine(hexDiagonal(a)) ;     }     // This code is contributed by Subhadeep}
<?php// PHP Program to find the diagonal// of a regular hexagon // Function to find the diagonal// of a regular hexagonfunction hexDiagonal($a){     // Side cannot be negative    if ($a < 0)        return -1;     // Length of the diagonal    $d = 1.73 * $a;    return $d;} // Driver code$a = 9;echo hexDiagonal($a), "\n"; // This code is contributed// by akt_mit?>
<script>// javascript Program to find the diagonal// of a regular hexagon // Function to find the diagonal// of a regular hexagonfunction hexDiagonal(a){     // Side cannot be negative    if (a < 0)        return -1;     // Length of the diagonal    var d = 1.73 * a;    return d;}         // Driver codevar a = 9;document.write(hexDiagonal(a)) ; // This code contributed by Princi Singh</script>
15.57
 
ankthon
tufan_gupta2000
jit_t
Akanksha_Rai
princi singh
C++ Programs
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C++ Program for QuickSort
cin in C++
delete keyword in C++
CSV file management using C++
Shallow Copy and Deep Copy in C++
Closest Pair of Points using Divide and Conquer algorithm
How to check if two given line segments intersect?
How to check if a given point lies inside or outside a polygon?
Find if two rectangles overlap
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)