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