Q:

Java program to calculate area of Hexagon

belongs to collection: Java Basic Programs

0

Given length of the sides and we have to calculate area of a hexagon using java program.

A hexagon has six sides of equal length, so we have to take input the length of the side, which will be considered length of all sides.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to find area of a hexagon in java

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to print prime numbers between given ... >>
<< Java program to compare two numbers with each othe...