Home » Java programs » Java basic programs

Java program to calculate area of Hexagon

Here, we are implementing a java program that will calculate the area of Hexagon. This will be calculated based on given length of the sides.
Submitted by IncludeHelp, on December 10, 2017

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.

(Read more: How to find area of a hexagon in Maths?)

Example:

    Input:
    Input the length : 2

    Output:
    The area of the hexagon is : 10.392304845413264

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.