给定一个整数N ,任务是找到一个N 边多边形的内角和。具有最少三个边和三个角的平面图形称为多边形。
例子:
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
方法: N边多边形的内角和由(N – 2) * 180 给出
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the sum of internal
// angles of an n-sided polygon
int sumOfInternalAngles(int n)
{
if (n < 3)
return 0;
return (n - 2) * 180;
}
// Driver code
int main()
{
int n = 5;
cout << sumOfInternalAngles(n);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to return the sum of internal
// angles of an n-sided polygon
static int sumOfInternalAngles(int n)
{
if (n < 3)
return 0;
return ((n - 2) * 180);
}
// Driver code
public static void main(String args[])
{
int n = 5;
System.out.print(sumOfInternalAngles(n));
}
}
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the sum of internal
// angles of an n-sided polygon
static int sumOfInternalAngles(int n)
{
if (n < 3)
return 0;
return ((n - 2) * 180);
}
// Driver code
public static void Main()
{
int n = 5;
Console.Write(sumOfInternalAngles(n));
}
}
Python
# Python3 implementation of the approach
# Function to return the sum of internal
# angles of an n-sided polygon
def sumOfInternalAngles(n):
if(n < 3):
return 0
return ((n - 2) * 180)
# Driver code
n = 5
print(sumOfInternalAngles(n))
PHP
Javascript
输出:
540
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。