98.7k views
3 votes
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 re polygon is

n * s2(s squar)
area = -----------------------------------------
4 * tan(pi/n)
write a function that return the area of a regular polygon using the following header.
double area(int n, double side.
write a main function that prompts the user to enter the number of sides and the side of a regular polygon, and display the area.
The program needs to allow the user to run the calculations as needed until the user chooses to end the program.

1 Answer

2 votes

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n;

double side;

char choice;

while(true){

System.out.print("Enter the number of sides: ");

n = input.nextInt();

System.out.print("Enter the side of a regular polygon: ");

side = input.nextDouble();

System.out.println("Area is: " + area(n, side));

System.out.print("Do you want to continue(y/n): ");

choice = input.next().charAt(0);

if(choice == 'n')

break;

}

}

public static double area(int n, double side){

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

return area;

}

}

Step-by-step explanation:

Create a function named area that takes two parameters, n and side

Calculate the area using the given formula

Return the area

In the main:

Create an indefinite while loop. Inside the loop:

Ask the user to enter the n and side

Call the area() function passing the n and side as parameters and print the result

Ask the user to continue or not. If the user enters "n", stop the loop

User MooMoo Cha
by
5.7k points