Saturday, June 21, 2014

Java Exercise Nr. 49 (Mathematical Functions)

The area of a hexagon can be computed using the following formula ( s is the length of a side):

Write a program that prompts the user to enter the side of a hexagon and displays its area.

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 = (6 * Math.pow(side, 2)) / (4 * Math.tan(Math.toRadians(30)));

System.out.printf("The area of the hexagon is %4.2f.", area);
}

1 comment:

  1. Very nice post.Thanks for sharing your knowledge to us.This is very helpfull programming post.
    Advantages Of DBMS Over File Management System

    ReplyDelete

Author