Java: Create the area of a pentagon
Java Method: Exercise-14 with Solution
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-exercises/method/java-method-exercise-14.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics