Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. 35. Write a Java program to compute the area of a polygon. Go to the editor
  2. Area of a polygon = (n*s^2)/(4*tan(π/n))
  3. where n is n-sided polygon and s is the length of a side
  4. Input Data:
  5. Input the number of sides on the polygon: 7
  6. Input the length of one of the sides: 6
  7. Expected Output
  8.  
  9. The area is: 130.82084798405722
  10.  
  11. public static void exercise35(){
  12. int number1 = integerInput(); //set to 7, length
  13. int sides = integerInput(); //set to 6, sides
  14.  
  15. double area = (sides * (number1 * number1)) / (4.0 * Math.tan((Math.PI / sides)));
  16.  
  17. System.out.println("The area of a polygon with " + sides + " sides of length " + number1 + " = " + area);
  18. }
  19.  
  20. import java.util.Scanner;
  21.  
  22. public class Exercise35 {
  23.  
  24. public static void main(String[] args) {
  25.  
  26. Scanner input = new Scanner(System.in);
  27. System.out.print("Input the number of sides on the polygon: ");
  28. int ns = input.nextInt();
  29. System.out.print("Input the length of one of the sides: ");
  30. double side = input.nextDouble();
  31. System.out.print("The area is: " + polygonArea(ns, side)+"n");
  32. }
  33. public static double polygonArea(int ns, double side) {
  34. return (ns * (side * side)) / (4.0 * Math.tan((Math.PI / ns)));
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement