Area of a n-sided regular polygon with given side length - GeeksforGeeks

17 Mar, 2021
Given a regular polygon of N sides with side length a. The task is to find the area of the polygon. Examples:  
Input : N = 6, a = 9
Output : 210.444

Input : N = 7, a = 8
Output : 232.571
 
 
Approach: In the figure above, we see the polygon can be divided into N equal triangles. Looking into one of the triangles, we see that the whole angle at the center can be divided into = 360/NSo, angle t = 180/n Now, tan(t) = a/2*hSo, h = a/(2*tan(t))Area of each triangle = (base * height)/2 = a * a/(4*tan(t)) So, area of the polygon,  
A = n * (area of one triangle) = a2 * n/(4tan t)
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to find the area of a regular// polygon with given side length #include <bits/stdc++.h>using namespace std; // Function to find the area// of a regular polygonfloat polyarea(float n, float a){    // Side and side length cannot be negative    if (a < 0 && n < 0)        return -1;     // Area    // degree converted to radians    float A = (a * a * n) / (4 * tan((180 / n) * 3.14159 / 180));     return A;} // Driver codeint main(){    float a = 9, n = 6;     cout << polyarea(n, a) << endl;     return 0;}
// Java  Program to find the area of a regular// polygon with given side length import java.io.*; class GFG {    // Function to find the area// of a regular polygonstatic float polyarea(float n, float a){    // Side and side length cannot be negative    if (a < 0 && n < 0)        return -1;     // Area    // degree converted to radians    float A = (a * a * n) /(float) (4 * Math.tan((180 / n) * 3.14159 / 180));     return A;} // Driver code     public static void main (String[] args) {    float a = 9, n = 6;     System.out.println( polyarea(n, a));    }}// This code is contributed by inder_verma..
# Python 3 Program to find the area# of a regular polygon with given# side lengthfrom math import tan # Function to find the area of a# regular polygondef polyarea(n, a):         # Side and side length cannot    # be negative    if (a < 0 and n < 0):        return -1     # Area degree converted to radians    A = (a * a * n) / (4 * tan((180 / n) *                      3.14159 / 180))     return A # Driver codeif __name__ == '__main__':    a = 9    n = 6     print('{0:.6}'.format(polyarea(n, a))) # This code is contributed by# Shashank_sharma
// C# Program to find the area of a regular// polygon with given side lengthusing System; class GFG{ // Function to find the area// of a regular polygonstatic float polyarea(float n, float a){    // Side and side length cannot be negative    if (a < 0 && n < 0)        return -1;     // Area    // degree converted to radians    float A = (a * a * n) / (float)(4 * Math.Tan((180 / n) *                                           3.14159 / 180));     return A;} // Driver codepublic static void Main (){    float a = 9, n = 6;         Console.WriteLine(polyarea(n, a));}} // This code is contributed// by Akanksha Rai
<?php// PHP Program to find the area of a regular// polygon with given side length // Function to find the area// of a regular polygonfunction polyarea($n, $a){    // Side and side length cannot    // be negative    if ($a < 0 && $n < 0)        return -1;     // Area    // degree converted to radians    $A = ($a * $a * $n) / (4 * tan((180 / $n) *                              3.14159 / 180));     return $A;} // Driver code$a = 9 ;$n = 6 ; echo round(polyarea($n, $a), 3); // This code is contributed by Ryuga?>
<script>// javascript  Program to find the area of a regular// polygon with given side length // Function to find the area// of a regular polygonfunction polyarea(n , a){     // Side and side length cannot be negative    if (a < 0 && n < 0)        return -1;     // Area    // degree converted to radians    var A = (a * a * n) / (4 * Math.tan((180 / n) * 3.14159 / 180));     return A;} // Driver codevar a = 9, n = 6;document.write( polyarea(n, a).toFixed(5)); // This code contributed by Princi Singh</script>
210.444
 
inderDuMCA
Shashank_Sharma
Akanksha_Rai
ankthon
amit143katiyar
school-programming
C++ Programs
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C++ Program for QuickSort
Shallow Copy and Deep Copy in C++
delete keyword in C++
Passing a function as a parameter in C++
cin in C++
Closest Pair of Points using Divide and Conquer algorithm
How to check if a given point lies inside or outside a polygon?
How to check if two given line segments intersect?
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Find if two rectangles overlap