Find length of Diagonal of Hexagon - GeeksforGeeks

08 Mar, 2022
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*aBelow is the implementation of the above approach:  
C++
C
Java
Python3
C#
PHP
Javascript
// C++ Program to find the diagonal// of the hexagon#include <bits/stdc++.h>using namespace std; // Function to find the diagonal// of the hexagonfloat hexadiagonal(float a){    // side cannot be negative    if (a < 0)        return -1;     // diagonal of the hexagon    return 2 * a;} // Driver codeint main(){    float a = 4;     cout << hexadiagonal(a) << endl;     return 0;}
// C Program to find the diagonal// of the hexagon#include <stdio.h> // Function to find the diagonal// of the hexagonfloat hexadiagonal(float a){    // side cannot be negative    if (a < 0)        return -1;      // diagonal of the hexagon    return 2 * a;}  // Driver codeint main(){    float a = 4;    printf("%f\n",hexadiagonal(a));    return 0;}
// Java Program to find the diagonal// of the hexagon import java.io.*; class GFG { // Function to find the diagonal// of the hexagonstatic 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
# Python3 Program to find the diagonal# of the hexagon # Function to find the diagonal# of the hexagondef hexadiagonal(a):     # side cannot be negative    if (a < 0):        return -1     # diagonal of the hexagon    return 2 * a  # Driver codeif __name__=='__main__':    a = 4    print(hexadiagonal(a)) # This code is contributed by# Shivi_Aggarwal
// C# Program to find the diagonal// of the hexagonusing 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 codepublic static void Main(){   float a = 4;   Console.WriteLine( hexadiagonal(a));}} // This code is contributed// by anuj_67..
<?php// PHP Program to find the diagonal// of the hexagon // Function to find the diagonal// of the hexagonfunction 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.. ?>
<script>// javascript Program to find the diagonal// of the hexagon // Function to find the diagonal// of the hexagonfunction hexadiagonal(a){     // side cannot be negative    if (a < 0)        return -1;     // diagonal of the hexagon    return 2 * a;} // Driver code var a = 4;document.write( hexadiagonal(a)); // This code is contributed by 29AjayKumar</script>
8
 
Shashank12
vt_m
Shivi_Aggarwal
29AjayKumar
kothavvsaakash
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
CSV file management using C++
delete keyword in C++
cin in 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?
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Find if two rectangles overlap