Program to find the Perimeter of a Regular Polygon
Last Updated :
21 Aug, 2022
Given the number of sides 'n' and the length of side 's' of a regular polygon, the task is to find out the Perimeter of this polygon.
Examples:
Input: n = 7, s = 10
Output: Perimeter : 70
Since the sides are 7,
Hence the given polygon is Heptagon.
Therefore. Perimeter = 7*10 = 70
Input: n = 5, s = 2.5
Output: Perimeter : 12.5
Since the sides are 5,
Hence the given polygon is Pentagon.
Therefore. Perimeter = 5*2.5 = 12.5
Approach: In geometry, a regular polygon is a closed figure with all sides equal. It is a two-dimensional figure.
Perimeter of a Regular Polygon = Number of sides * Length of each side


Below is the implementation of the above approach:
C++
// C++ program to find the
// perimeter of a regular polygon
#include <iostream>
using namespace std;
// Function to calculate the perimeter
float Perimeter(float s, int n)
{
float perimeter = 1;
// Calculate Perimeter
perimeter = n * s;
return perimeter;
}
// driver code
int main()
{
// Get the number of sides
int n = 5;
// Get the length of side
float s = 2.5, peri;
// find perimeter
peri = Perimeter(s, n);
cout << "Perimeter of Regular Polygon"
<< " with " << n << " sides of length "
<< s << " = " << peri << endl;
return 0;
}
C
// C program to find the
// perimeter of a regular polygon
#include <stdio.h>
// Function to calculate the perimeter
float Perimeter(float s, int n)
{
float perimeter = 1;
// Calculate Perimeter
perimeter = n * s;
return perimeter;
}
// driver code
int main()
{
// Get the number of sides
int n = 5;
// Get the length of side
float s = 2.5, peri;
// find perimeter
peri = Perimeter(s, n);
printf("Perimeter of Regular Polygon\n"
" with %d sides of length %f = %f\n",
n, s, peri);
return 0;
}
Java
// Java program to find the
// perimeter of a regular polygon
class GFG {
// Function to calculate the perimeter
static double Perimeter(double s, int n)
{
double perimeter = 1;
// Calculate Perimeter
perimeter = n * s;
return perimeter;
}
// Driver method
public static void main(String[] args)
{
// Get the number of sides
int n = 5;
// Get the length of side
double s = 2.5, peri;
// find perimeter
peri = Perimeter(s, n);
System.out.println("Perimeter of Regular Polygon"
+ " with " + n + " sides of length "
+ s + " = " + peri);
}
}
Python3
# Python3 program to find the
# perimeter of a regular polygon
# Function to calculate the perimeter
def Perimeter(s, n):
perimeter = 1
# Calculate Perimeter
perimeter = n * s
return perimeter
# driver code
if __name__== '__main__':
# Get the number of sides
n = 5
#Get the length of side
s = 2.5
# find perimeter
peri = Perimeter(s, n)
print("Perimeter of Regular Polygon with"
,n,"sides of length",s,"=",peri)
# This code is contributed by
# SURENDRA_GANGWAR
C#
// C# program to find the
// perimeter of a regular polygon
using System;
class GFG
{
// Function to calculate the perimeter
static double Perimeter(double s, int n)
{
double perimeter = 1;
// Calculate Perimeter
perimeter = n * s;
return perimeter;
}
// Driver Code
static public void Main ()
{
// Get the number of sides
int n = 5;
// Get the length of side
double s = 2.5, peri;
// find perimeter
peri = Perimeter(s, n);
Console.WriteLine("Perimeter of Regular Polygon" +
" with " + n + " sides of length " +
s + " = " + peri);
}
}
// This code is contributed by Sachin
PHP
<?php
// PHP program to find the
// perimeter of a regular polygon
// Function to calculate the perimeter
function Perimeter($s, $n)
{
$perimeter = 1;
// Calculate Perimeter
$perimeter = $n * $s;
return $perimeter;
}
// driver code
// Get the number of sides
$n = 5;
// Get the length of side
$s = 2.5;
// find perimeter
$peri = Perimeter($s, $n);
echo "Perimeter of Regular Polygon"
," with ", $n," sides of length "
,$s," = ",$peri;
// This code is contributed by ANKITRAI1
?>
JavaScript
<script>
// Javascript program to find the
// perimeter of a regular polygon
// Function to calculate the perimeter
function Perimeter(s, n)
{
var perimeter = 1;
// Calculate Perimeter
perimeter = n * s;
return perimeter;
}
// Driver code
// Get the number of sides
var n = 5;
// Get the length of side
var s = 2.5, peri;
// find perimeter
peri = Perimeter(s, n);
document.write("Perimeter of Regular Polygon<br/>" +
" with " + n + " sides of length " +
s.toFixed(6) + " = " + peri.toFixed(6));
// This code is contributed by gauravrajput1
</script>
Output: Perimeter of Regular Polygon
with 5 sides of length 2.500000 = 12.500000
Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Similar Reads
Program to find the Circumcircle of any regular polygon Given a n-sided polygon with side length a. The task is to find the area of the circumcircle of the polygon.Examples:Input: n = 10, a = 3 Output: 1.99737Input: n = 5, a = 6 Output: 3.02487Approach: A regular n-gon divides the circle into n pieces, so the central angle of the triangle is a full circl
4 min read
Program to find the Interior and Exterior Angle of a Regular Polygon Given the number of sides n of a regular polygon. The task is to find out the Interior angle and Exterior angle of the polygon.Examples: Input : n = 6 Output : Interior angle: 120 Exterior angle: 60 Input : n = 10 Output: Interior angle: 144 Exterior angle: 36 Interior angle: The angle between two a
4 min read
Program to find the Area and Perimeter of a Semicircle Given the radius of the semicircle as r, the task is to find out the Area and Perimeter of that semicircle.Examples: Input: r = 10 Output: Area = 157.00, Perimeter = 31.4 Input: r = 25 Output: Area =981.250000, Perimeter = 78.500000 Approach: In mathematics, a semicircle is a one-dimensional locus o
5 min read
Program to find Area of Triangle inscribed in N-sided Regular Polygon Given the triangle inscribed in an N-sided regular polygon with given side length, formed using any 3 vertices of the polygon, the task is to find the area of this triangle. Examples: Input: N = 6, side = 10 Output: 129.904 Input: N = 8, side = 5 Output: 45.2665 Approach: Consider the 1st example: G
6 min read
Program to find Perimeter / Circumference of Square and Rectangle The circumference of a figure is the sum of all the side lengths. To calculate the circumference of square, length of one of the side is required as all sides are equal. To calculate the circumference of rectangle, length and breadth of rectangle is required. Circumference of a Square:  The circumf
5 min read