Posted on by Kalkicode
Code Geometric

Find the area of a Hexagon

The problem involves calculating the area of a regular hexagon, a six-sided polygon where all sides and angles are equal. The area of a hexagon can be determined using a formula that takes the length of a side as input. Calculating the area of a hexagon is a geometric calculation used in various fields such as mathematics, engineering, and design.

Problem Statement

Given the length of a side of a regular hexagon, we need to calculate and output the area of the hexagon. The side length is provided as input, and we are required to use the formula for the area of a hexagon to find the area.

Example

Consider a regular hexagon with a side length of 7 units. This hexagon can be visualized as follows:


       /\
      /  \
     /    \
     \    /
      \  /
       \/
       7.0

Idea to Solve

To solve this problem, we can follow these steps:

  1. Calculate the area of the hexagon using the formula: (3 * √3 * (side * side)) / 2.
  2. Use the provided value for the side length to compute the area.

Pseudocode

function hexagon_area(side):
    area = (3 * sqrt(3) * (side * side)) / 2
    return area

for each test case:
    side = hexagon side length
    result = hexagon_area(side)
    print("Hexagon Area of side", side, "is :", result)

Algorithm Explanation

  1. For each test case, calculate the area of the hexagon using the provided formula.
  2. Print the hexagon's side length and the calculated area.

Code Solution

/*
  C Program 
  Find the area of a Hexagon 
*/
#include <stdio.h>

#include <math.h>

//Calculate area of hexagon by given side length
void hexagon_area(double side)
{
	// Formula of hexagon area
	// (3 √3(side)² ) / 2
	// Calculate area of hexagon
	double area = (3 * sqrt(3) * (side * side)) / 2;
	//Display result
	printf("Hexagon Area of side %lf is : %lf\n", side, area);
}
int main()
{
	//Test Case
	hexagon_area(7);
	hexagon_area(3);
	hexagon_area(5.2);
	return 0;
}

Output

Hexagon Area of side 7.000000 is : 127.305734
Hexagon Area of side 3.000000 is : 23.382686
Hexagon Area of side 5.200000 is : 70.251981
// Java Program
// Find the area of a Hexagon 
class Hexagon
{
	//Calculate area of hexagon by given side length
	public void hexagon_area(double side)
	{
		// Formula of hexagon area
		// (3 √3(side)² ) / 2
		// Calculate area of hexagon
		double area = (3 * Math.sqrt(3) * (side * side)) / 2;
		System.out.print("Hexagon Area of side " + side + " is : " + area + "\n");
	}
	public static void main(String[] args)
	{
		Hexagon obj = new Hexagon();
		//Simple Case
		obj.hexagon_area(7);
		obj.hexagon_area(3);
		obj.hexagon_area(5.2);
	}
}

Output

Hexagon Area of side 7.0 is : 127.30573435631248
Hexagon Area of side 3.0 is : 23.382685902179844
Hexagon Area of side 5.2 is : 70.25198075499367
// C++ Program
// Find the area of a Hexagon 
#include<iostream>
#include<math.h>
using namespace std;
class Hexagon
{
	public:
		//Calculate area of hexagon by given side length
		void hexagon_area(double side)
		{
			// Formula of hexagon area
			// (3 √3(side)² ) / 2
			// Calculate area of hexagon
			double area = (3 * sqrt(3) * (side * side)) / 2;
			cout << "Hexagon Area of side " << side << " is : " << area << "\n";
		}
};
int main()
{
	Hexagon obj =  Hexagon();
	//Simple Case
	obj.hexagon_area(7);
	obj.hexagon_area(3);
	obj.hexagon_area(5.2);
	return 0;
}

Output

Hexagon Area of side 7 is : 127.306
Hexagon Area of side 3 is : 23.3827
Hexagon Area of side 5.2 is : 70.252
// C# Program
// Find the area of a Hexagon 
using System;
class Hexagon
{
	//Calculate area of hexagon by given side length
	public void hexagon_area(double side)
	{
		// Formula of hexagon area
		// (3 √3(side)² ) / 2
		// Calculate area of hexagon
		double area = (3 * Math.Sqrt(3) * (side * side)) / 2;
		Console.Write("Hexagon Area of side " + side + " is : " + area + "\n");
	}
	public static void Main(String[] args)
	{
		Hexagon obj = new Hexagon();
		//Simple Case
		obj.hexagon_area(7);
		obj.hexagon_area(3);
		obj.hexagon_area(5.2);
	}
}

Output

Hexagon Area of side 7 is : 127.305734356312
Hexagon Area of side 3 is : 23.3826859021798
Hexagon Area of side 5.2 is : 70.2519807549937
<?php
// Php Program
// Find the area of a Hexagon 
class Hexagon
{
	//Calculate area of hexagon by given side length
	public	function hexagon_area($side)
	{
		// Formula of hexagon area
		// (3 √3(side)² ) / 2
		// Calculate area of hexagon
		$area = (3 * sqrt(3) * ($side * $side)) / 2;
		echo "Hexagon Area of side ". $side ." is : ". $area ."\n";
	}
}

function main()
{
	$obj = new Hexagon();
	//Simple Case
	$obj->hexagon_area(7);
	$obj->hexagon_area(3);
	$obj->hexagon_area(5.2);
}
main();

Output

Hexagon Area of side 7 is : 127.30573435631
Hexagon Area of side 3 is : 23.38268590218
Hexagon Area of side 5.2 is : 70.251980754994
// Node Js Program
// Find the area of a Hexagon 
class Hexagon
{
	//Calculate area of hexagon by given side length
	hexagon_area(side)
	{
		// Formula of hexagon area
		// (3 √3(side)² ) / 2
		// Calculate area of hexagon
		var area = (3 * Math.sqrt(3) * (side * side)) / 2;
		process.stdout.write("Hexagon Area of side " + side + " is : " + area + "\n");
	}
}

function main()
{
	var obj = new Hexagon();
	//Simple Case
	obj.hexagon_area(7);
	obj.hexagon_area(3);
	obj.hexagon_area(5.2);
}
main();

Output

Hexagon Area of side 7 is : 127.30573435631248
Hexagon Area of side 3 is : 23.382685902179844
Hexagon Area of side 5.2 is : 70.25198075499367
#  Python 3 Program
#  Find the area of a Hexagon 

import math

class Hexagon :
	# Calculate area of hexagon by given side length
	def hexagon_area(self, side) :
		#  Formula of hexagon area
		#  (3 √3(side)² ) / 2
		#  Calculate area of hexagon
		area = (3 * math.sqrt(3) * (side * side)) / 2
		print("Hexagon Area of side ", side ," is : ", area ,"\n", end = "")
	

def main() :
	obj = Hexagon()
	# Simple Case
	obj.hexagon_area(7)
	obj.hexagon_area(3)
	obj.hexagon_area(5.2)

if __name__ == "__main__": main()

Output

Hexagon Area of side  7  is :  127.30573435631248
Hexagon Area of side  3  is :  23.382685902179844
Hexagon Area of side  5.2  is :  70.25198075499367
#  Ruby Program
#  Find the area of a Hexagon 
class Hexagon

	# Calculate area of hexagon by given side length
	def hexagon_area(side)
	
		#  Formula of hexagon area
		#  (3 √3(side)² ) / 2
		#  Calculate area of hexagon
		area = (3 * Math.sqrt(3) * (side * side)) / 2
		print("Hexagon Area of side ", side ," is : ", area ,"\n")
	end
end
def main()

	obj = Hexagon.new()
	# Simple Case
	obj.hexagon_area(7)
	obj.hexagon_area(3)
	obj.hexagon_area(5.2)
end
main()

Output

Hexagon Area of side 7 is : 127.30573435631248
Hexagon Area of side 3 is : 23.382685902179844
Hexagon Area of side 5.2 is : 70.25198075499367
// Scala Program
// Find the area of a Hexagon 
class Hexagon
{
	//Calculate area of hexagon by given side length
	def hexagon_area(side: Double): Unit = {
		// Formula of hexagon area
		// (3 √3(side)² ) / 2
		// Calculate area of hexagon
		var area: Double = (3 * Math.sqrt(3) * (side * side)) / 2;
		print("Hexagon Area of side " + side + " is : " + area + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: Hexagon = new Hexagon();
		//Simple Case
		obj.hexagon_area(7);
		obj.hexagon_area(3);
		obj.hexagon_area(5.2);
	}
}

Output

Hexagon Area of side 7.0 is : 127.30573435631248
Hexagon Area of side 3.0 is : 23.382685902179844
Hexagon Area of side 5.2 is : 70.25198075499367
// Swift Program
// Find the area of a Hexagon 
import Foundation
class Hexagon
{
	//Calculate area of hexagon by given side length
	func hexagon_area(_ side: Double)
	{
		// Formula of hexagon area
		// (3 √3(side)² ) / 2
		// Calculate area of hexagon
		let area: Double = (3 * sqrt(3) * (side * side)) / 2;
		print("Hexagon Area of side ", side ," is : ", area ,"\n", terminator: "");
	}
}
func main()
{
	let obj: Hexagon = Hexagon();
	//Simple Case
	obj.hexagon_area(7);
	obj.hexagon_area(3);
	obj.hexagon_area(5.2);
}
main();

Output

Hexagon Area of side  7.0  is :  127.305734356312
Hexagon Area of side  3.0  is :  23.3826859021798
Hexagon Area of side  5.2  is :  70.2519807549937

Output Explanation

The provided code runs three test cases and calculates the areas of regular hexagons with different side lengths. The output includes the hexagon's side length and the corresponding calculated areas.

Time Complexity

The time complexity of this code is O(1) for each test case. The calculations involve basic arithmetic operations and square root computations, which take constant time. Therefore, the overall time complexity remains constant for each test case.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment