Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Java Method Exercises: Create the area of a pentagon

Java Method: Exercise-14 with Solution

Write a Java method to create the area of a pentagon.

Pictorial Presentation:

Java Method Exercises: Create the area of a pentagon

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:

Flowchart: Create the area of a pentagon

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.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe