Saturday, June 21, 2014

Java Exercise Nr. 50 (Mathematical Functions)

A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon is:

Here, s is the length of a side. Write a program that prompts the user to enter the number of sides and their length of a regular polygon and displays its area. 

Solution:

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n;
double side, area;

System.out.print("Enter the number of sides:");
n = s.nextInt();

System.out.print("Enter the side:");
side = s.nextDouble();

area = (n * Math.pow(side, 2)) / (4 * Math.tan(Math.toRadians(Math.PI / n)));

System.out.print("The area of the polygon is " + area);
}

No comments:

Post a Comment

Author