Friday 13 May 2016

Chapter 2 Exercise 16, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

2.16 (Geometry: area of a hexagon) 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. Here is a sample run:
    
    Enter the side: 5.5
    The area of the hexagon is 78.5895



import java.util.Scanner;
 
public class ProgrammingEx2_16 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter the side:");
  double s = input.nextDouble();
  double area = (3 * Math.sqrt(3) / 2) * Math.pow(s, 2);
  System.out.print("The area of the hexagon is " + area);
 
 }
 
}

No comments :

Post a Comment