# How to calculate the area of polygon in Java

In this tutorial, we will learn how to calculate the area of a polygon in Java. Where we take no of sides and length of the side of a polygon as an input.

## Area of  Polygon in Java

What is a polygon?
A polygon is any 2-dimensional shape formed with straight lines. Triangles, quadrilaterals, pentagons, and hexagons are all examples of polygons.

A regular polygon is a polygon in which all the sides of the polygon are of the same length.
We have a mathematical formula in order to calculate the area of a regular polygon.
Area=(ln*ln*n/4*(tan(180/n))
Where,
ln=length of the side of a polygon
n=number of sides in a polygon
tan=tanget function in degrees

1. In the code first, we have  taken the input as the number of sides of a polygon
```   Scanner sc=new Scanner(System.in);
//Accept number of sides
System.out.println("Enter the no.of sides in polygon:");
int n=sc.nextInt();```
2. Next, we  take the length of the sides as an input
```//Accept length of sides
System.out.println("Enter the length of side in polygon:");
double ln=sc.nextDouble();```
3. In order to calculate the tangent we have to convert the angle into radians, so we use Math.toRadians() function to do so. After that, we calculate the tangent using Math.tan() function from java.lang.Math package.
```double angle= Math.toRadians(180/n);
angle=Math.tan(angle);```
4. Now we calculate the area by applying our formulae of a regular polygon.
```double area=((ln*ln*n)/(4*angle));
System.out.print("Area of polygon is:"+area);```

Here is the code to calculate the Area

```import  java.lang.*;
import java.util.Scanner;
class Area
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

//Accept number of sides
System.out.print("Enter the no.of sides in polygon:");
int n=sc.nextInt();

//Accept length of sides
System.out.print("Enter the length of side in polygon:");
double ln=sc.nextDouble();

angle=Math.tan(angle);

double area=((ln*ln*n)/(4*angle));
System.out.print("Area of polygon is:"+area);

}
}```

Output:

```Enter the no.of sides in polygon: 6
Enter the length of side in polygon: 6
Area of polygon is: 93.53074360871938```

This is how we can find out or calculate the area of a polygon in Java.