Thursday, June 19, 2014

Elementary Programming; Exercise No. 14

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);
}

3 comments:

  1. hello. I was wondering if you could explain where the 0.5 comes from: Math.pow(3, 0.5). thank you.

    ReplyDelete
    Replies
    1. I 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

Author