Write a Java method to create the area of a pentagon


This Java program calculates the area of a regular pentagon (a polygon with five sides) using the input provided by the user. Here is a breakdown of how the program works:

  • First, the program imports the Scanner class from the java.util package to read user input.
  • Then, the main method is defined, which is the entry point for the program.
  • Inside the main method, a Scanner object called "input" is created to read user input.
  • The program prompts the user to enter the number of sides of the pentagon and reads the input as an integer variable "n".
  • The program prompts the user to enter the length of one side of the pentagon and reads the input as a double variable "s".
  • The program calculates the area of the pentagon using the formula: (n * s * s) / (4 * Math.tan(Math.PI/n)) , where "n" is the number of sides and "s" is the length of one side. The Math.tan() method is used to calculate the tangent of an angle in radians, and Math.PI is a constant representing the value of pi.
  • Finally, the program prints the calculated area of the pentagon to the console using System.out.println().

Source Code

import java.util.Scanner;
public class Area_Pentagon
{ 
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the Number of Sides : ");
		int n = input.nextInt();
		System.out.print("Enter the Side : ");
		double s = input.nextDouble();
 
		System.out.println("Area of the Pentagon is " + (n * s * s) / (4 * Math.tan(Math.PI/n)));
	}
}

Output

Enter the Number of Sides : 3
Enter the Side : 63
Area of the Pentagon is 1718.627413810219

Example Programs