Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 35. Write a Java program to compute the area of a polygon. Go to the editor
- Area of a polygon = (n*s^2)/(4*tan(π/n))
- where n is n-sided polygon and s is the length of a side
- Input Data:
- Input the number of sides on the polygon: 7
- Input the length of one of the sides: 6
- Expected Output
- The area is: 130.82084798405722
- public static void exercise35(){
- int number1 = integerInput(); //set to 7, length
- int sides = integerInput(); //set to 6, sides
- double area = (sides * (number1 * number1)) / (4.0 * Math.tan((Math.PI / sides)));
- System.out.println("The area of a polygon with " + sides + " sides of length " + number1 + " = " + area);
- }
- import java.util.Scanner;
- public class Exercise35 {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.print("Input the number of sides on the polygon: ");
- int ns = input.nextInt();
- System.out.print("Input the length of one of the sides: ");
- double side = input.nextDouble();
- System.out.print("The area is: " + polygonArea(ns, side)+"n");
- }
- public static double polygonArea(int ns, double side) {
- return (ns * (side * side)) / (4.0 * Math.tan((Math.PI / ns)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement