Sum of internal angles of a Polygon
Last Updated :
08 Mar, 2022
Given an integer N, the task is to find the sum of interior angles of an N-sided polygon. A plane figure having a minimum of three sides and angles is called a polygon.
Examples:
Input: N = 3
Output: 180
3-sided polygon is a triangle and the sum
of the interior angles of a triangle is 180.
Input: N = 6
Output: 720
Approach: The sum of internal angles of a polygon with N sides is given by (N – 2) * 180
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfInternalAngles( int n)
{
if (n < 3)
return 0;
return (n - 2) * 180;
}
int main()
{
int n = 5;
cout << sumOfInternalAngles(n);
return 0;
}
|
Java
class GFG {
static int sumOfInternalAngles( int n)
{
if (n < 3 )
return 0 ;
return ((n - 2 ) * 180 );
}
public static void main(String args[])
{
int n = 5 ;
System.out.print(sumOfInternalAngles(n));
}
}
|
C#
using System;
class GFG {
static int sumOfInternalAngles( int n)
{
if (n < 3)
return 0;
return ((n - 2) * 180);
}
public static void Main()
{
int n = 5;
Console.Write(sumOfInternalAngles(n));
}
}
|
Python
def sumOfInternalAngles(n):
if (n < 3 ):
return 0
return ((n - 2 ) * 180 )
n = 5
print (sumOfInternalAngles(n))
|
PHP
<?php
function sumOfInternalAngles( $n )
{
if ( $n < 3)
return 0;
return (( $n - 2) * 180);
}
$n = 5;
echo (sumOfInternalAngles( $n ));
?>
|
Javascript
<script>
function sumOfInternalAngles(n)
{
if (n < 3)
return 0;
return (n - 2) * 180;
}
let n = 5;
document.write(sumOfInternalAngles(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...