Write a program that prompts the user to enter the side of a hexagon and displays its area. The formula for computing the area of a hexagon is:
where s is the length of a side.
Solution:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
double side, area;
System.out.print("Enter the side:");
side = s.nextDouble();
area = ((3 * Math.pow(3, 0.5) / 2)) * Math.pow(side, 2);
System.out.print("The area of the hexagon is " + area);
}
Subscribe to:
Post Comments (Atom)
Thank you!
ReplyDeletehello. I was wondering if you could explain where the 0.5 comes from: Math.pow(3, 0.5). thank you.
ReplyDeleteI got it. I kinda forgot the simple math. Math.pow(3,05) = 3^0.5 = square root of 3 = 1.73205080757. in case others will find the same problem.
Delete