Program to find the Perimeter of a Regular Polygon

18 Jun, 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
Java
Python3
C#
PHP
Javascript
// C++ program to find the// perimeter of a regular polygon #include <iostream>using namespace std; // Function to calculate the perimeterfloat Perimeter(float s, int n){    float perimeter = 1;     // Calculate Perimeter    perimeter = n * s;     return perimeter;} // driver codeint 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 program to find the// perimeter of a regular polygon #include <stdio.h> // Function to calculate the perimeterfloat Perimeter(float s, int n){    float perimeter = 1;     // Calculate Perimeter    perimeter = n * s;     return perimeter;} // driver codeint 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 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 program to find the# perimeter of a regular polygon  # Function to calculate the perimeterdef Perimeter(s, n):    perimeter = 1    # Calculate Perimeter    perimeter = n * s     return perimeter # driver codeif __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# program to find the// perimeter of a regular polygonusing System; class GFG{// Function to calculate the perimeterstatic double Perimeter(double s, int n){    double perimeter = 1;     // Calculate Perimeter    perimeter = n * s;     return perimeter;} // Driver Codestatic 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 program to find the// perimeter of a regular polygon // Function to calculate the perimeterfunction 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?>
<script> // Javascript program to find the// perimeter of a regular polygon // Function to calculate the perimeterfunction Perimeter(s, n){    var perimeter = 1;         // Calculate Perimeter    perimeter = n * s;         return perimeter;} // Driver code // Get the number of sidesvar n = 5; // Get the length of sidevar s = 2.5, peri; // find perimeterperi = 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>
Perimeter of Regular Polygon 
with 5 sides of length 2.500000 = 12.500000
 
Time Complexity: O(1)Auxiliary Space: O(1)
ankthon
SURENDRA_GANGWAR
Sach_Code
GauravRajput1
rajeev0719singh
singhh3010
area-volume-programs
Geometric
Mathematical
School Programming
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for distance between two points on earth
Optimum location of point to minimize total distance
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Check whether a given point lies inside a triangle or not
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays