Java: Create the area of a pentagon
Calculate Pentagon Area
Write a Java method to create a pentagon's area.
Pictorial Presentation:
Sample Solution:
Java Code:
import java.util.Scanner;
public class Exercise14 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input the number of sides: ");
int n = input.nextInt();
System.out.print("Input the side: ");
double side = input.nextDouble();
System.out.println("The area of the pentagon is " + pentagon_area(n, side));
}
public static double pentagon_area(int n, double s) {
return (n * s * s) / (4 * Math.tan(Math.PI/n));
}
}
Sample Output:
Input the number of sides: 5 Input the side: 6 The area of the pentagon is 61.93718642120281
Flowchart:
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write Java methods to calculate the area of a triangle.
Next: Write a Java method to display the current date and time.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics