Java Program to Find Area and Perimeter of Pentagon

Program to Find Area of Pentagon

Before jumping into the program directly, let’s first see how we calculate area of pentagon.

Formula of Area of Pentagon: (sqrt(5*(5+2*sqrt(5)))*pow(a,2))/4.0

Formula of Perimeter of Pentagon: 5a

Where,

• a‘ represents the side length of Pentagon.
Example-To find area of pentagon

a=5.5
Area of Pentagon = (sqrt(5*(5+2*sqrt(5)))*pow(a,2))/4.0
= (sqrt(5*(5+2*sqrt(5)))*pow(5.5,2))/4.0
= 52.04444136781625
Example-To find perimeter of pentagon

a=5.5
Perimeter of Pentagon = 5a
= 5*5.5
= 27.5

Let’s see different ways to do it.

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Method-1: Using Static Value

In this method, the side length of pentagon is already defined in the program. And the area and perimeter are calculated according to the formula by using this side length value.

So, let’s see the program to see how it actually works.

import java.util.Scanner;

public class Main
{

public static void main(String[] args)
{
//creating Scanner class object
Scanner sc=new Scanner(System.in);
//Enter side length of pentagon
System.out.println("Enter side length of Pentagon :");
double a = 5.5;
double area = (Math.sqrt(5*(5+2*Math.sqrt(5)))*Math.pow(a,2))/4.0;
double perimeter = (5*a);
System.out.println("Area of Pentagon = "+area);
System.out.println("Perimeter of Pentagon = "+perimeter);
}
}

Output:

Enter side length of Pentagon : 5.5
Area of Pentagon = 52.04444136781625
Perimeter of Pentagon = 27.5

Method-2: Using User Defined Value

In this method, the side length of pentagon is taken as user input. And the area and perimeter are calculated according to the formula by using this side length value.

So, let’s see the program to see how it actually works.

import java.util.Scanner;

public class Main
{

public static void main(String[] args)
{
//creating Scanner class object
Scanner sc=new Scanner(System.in);
//Enter side length of pentagon
System.out.println("Enter side length of Pentagon :");
int a = sc.nextInt();
double area = (Math.sqrt(5*(5+2*Math.sqrt(5)))*Math.pow(a,2))/4.0;
int perimeter = (5*a);
System.out.println("Area of Pentagon = "+area);
System.out.println("Perimeter of Pentagon = "+perimeter);
}
}

Output:

Enter side length of Pentagon : 5
Area of Pentagon = 43.01193501472417
Perimeter of Pentagon = 25