Area of a Hexagon
Last Updated :
17 Feb, 2023
A hexagon is a 6-sided, 2-dimensional geometric figure. The total of the internal angles of any hexagon is 720°. A regular hexagon has 6 rotational symmetries and 6 reflection symmetries. All internal angles are 120 degrees.
Examples :
Input: 4
Output: 41.5692
Input: 6
Output: 93.5307
Number of vertices: 6
Number of edges: 6
Internal angle: 120°
Area = (3 ?3(n)2 ) / 2
How does the formula work? There are mainly 6 equilateral triangles of side n and area of an equilateral triangle is (sqrt(3)/4) * n * n. Since in hexagon, there are total 6 equilateral triangles with side n, area of the hexagon becomes (3*sqrt(3)/2) * n * n
C++
#include <iostream>
#include <math.h>
using namespace std;
double hexagonArea( double s)
{
return ((3 * sqrt (3) *
(s * s)) / 2);
}
int main()
{
double s = 4;
cout << "Area : "
<< hexagonArea(s);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
public static double hexagonArea( double s)
{
return (( 3 * Math.sqrt( 3 ) *
(s * s)) / 2 );
}
public static void main(String[] args)
{
double s = 4 ;
System.out.print( "Area: " +
hexagonArea(s) );
}
}
|
Python3
import math
def hexagonArea(s):
return (( 3 * math.sqrt( 3 ) *
(s * s)) / 2 );
if __name__ = = "__main__" :
s = 4
print ( "Area:" , "{0:.4f}" .
format (hexagonArea(s)))
|
C#
using System;
class GFG
{
public static double hexagonArea( double s)
{
return ((3 * Math.Sqrt(3) *
(s * s)) / 2);
}
public static void Main()
{
double s = 4;
Console.WriteLine( "Area: " +
hexagonArea(s) );
}
}
|
PHP
<?php
function hexagonArea( $s )
{
return ((3 * sqrt(3) *
( $s * $s )) / 2);
}
$s = 4;
echo ( "Area : " );
echo (hexagonArea( $s ));
?>
|
Javascript
<script>
function hexagonArea(s)
{
return ((3 * Math.sqrt(3) *
(s * s)) / 2);
}
let s = 4;
document.write( "Area : "
+ hexagonArea(s));
</script>
|
Output :
Area: 41.5692
Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...