Write a python program to calculate the area of a regular polygon


This program calculates the area of a regular polygon with the given number of sides and length of a side using the formula:

area = (sides * length^2) / (4 * tan(pi/sides))
  • The program prompts the user to input the number of sides and length of a side, and stores these values in the variables sides and length, respectively.
  • It then uses the imported tan and pi functions from the math module to calculate the area using the formula above. The tan function returns the tangent of an angle, and the pi constant represents the value of pi (approximately 3.14159).
  • The calculated area is stored in the variable area, and rounded to four decimal places using the round function with a second argument of 4.
  • Finally, the program displays the result to the user with a message "Area of the Polygon : " followed by the calculated area using the print function.

Source Code

from math import tan, pi
sides = int(input("Enter the Number of Sides : "))
length = float(input("Enter the Length of a Side : "))
area = sides * (length ** 2) / (4 * tan(pi / sides))
res = round(area,4)
print("Area of the Polygon : ",res)

Output

Enter the Number of Sides : 3
Enter the Length of a Side : 45
Area of the Polygon :  876.8507

Example Programs