Friday, October 10, 2025
HomeData Modelling & AIDiagonal of a Regular Hexagon

Diagonal of a Regular Hexagon

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.38
Input: 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++




// 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 hexagon
float hexDiagonal(float a)
{
 
    // Side cannot be negative
    if (a < 0)
        return -1;
 
    // Length of the diagonal
    float d = 1.73 * a;
    return d;
}
 
// Driver code
int main()
{
    float a = 9;
    cout << hexDiagonal(a) << endl;
    return 0;
}


Java




// 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




# Python3 Program to find the diagonal
# of a regular hexagon
 
# Function to find the diagonal
# of a regular hexagon
def 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;
print(hexDiagonal(a));
 
# This code is contributed
# by Akanksha Rai


C#




// C# Program to find the diagonal 
// of a regular hexagon
using 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
// PHP Program to find the diagonal
// of a regular hexagon
 
// Function to find the diagonal
// of a regular hexagon
function 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
?>


Javascript




<script>
// javascript Program to find the diagonal
// of a regular hexagon
 
// Function to find the diagonal
// of a regular hexagon
function hexDiagonal(a)
{
 
    // Side cannot be negative
    if (a < 0)
        return -1;
 
    // Length of the diagonal
    var d = 1.73 * a;
    return d;
}
     
    // Driver code
var a = 9;
document.write(hexDiagonal(a)) ;
 
// This code contributed by Princi Singh
</script>


Output: 

15.57

 

Time Complexity: O(1)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7099 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS