Java Program to Calculate Area of Hexagon

You are currently viewing Java Program to Calculate Area of Hexagon

Java program to calculate area of Hexagon

import java.util.Scanner;

public class AreaOfHexagon 
{
	public static void main(String[] args) 
	{
		// create scanner class object.
		Scanner sc = new Scanner(System.in);
	
		// enter length of sides.
		System.out.print("Input the length : ");
		double s = sc.nextDouble();
		System.out.print("The area of the hexagon is : " + hexagonArea(s)+"\n");
	}

	// create function for calculating area.
	public static double hexagonArea(double s)
	{
	    return (6*(s*s))/(4*Math.tan(Math.PI/6));
	}
}

Output

First run:
Input the length : 10
The area of the hexagon is : 259.8076211353316

Second run:
Input the length : 2
The area of the hexagon is : 10.392304845413264

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply