Check if it is possible to create a polygon with a given angle
Last Updated :
31 Aug, 2022
Given an angle a where, 1\le a< 180 . The task is to check whether it is possible to make a regular polygon with all of its interior angle equal to a . If possible then print "YES", otherwise print "NO" (without quotes).
Examples:
Input: angle = 90
Output: YES
Polygons with sides 4 is
possible with angle 90 degrees.
Input: angle = 30
Output: NO
Approach: The Interior angle is defined as the angle between any two adjacent sides of a regular polygon.
It is given by \;Interior\;angle = \frac{180 \times (n-2)}{n}\; where, n is the number of sides in the polygon.
This can be written as \;a = \frac{180 \times (n-2)}{n}\; .
On rearranging terms we get, \;n = \frac{360}{180 - a}\; .
Thus, if n is an Integer the answer is "YES" otherwise, answer is "NO".
Below is the implementation of the above approach:
C++
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check whether it is possible
// to make a regular polygon with a given angle.
void makePolygon(float a)
{
// N denotes the number of sides
// of polygons possible
float n = 360 / (180 - a);
if (n == (int)n)
cout << "YES";
else
cout << "NO";
}
// Driver code
int main()
{
float a = 90;
// function to print the required answer
makePolygon(a);
return 0;
}
Java
class GFG
{
// Function to check whether
// it is possible to make a
// regular polygon with a given angle.
static void makePolygon(double a)
{
// N denotes the number of
// sides of polygons possible
double n = 360 / (180 - a);
if (n == (int)n)
System.out.println("YES");
else
System.out.println("NO");
}
// Driver code
public static void main (String[] args)
{
double a = 90;
// function to print
// the required answer
makePolygon(a);
}
}
// This code is contributed by Bilal
Python3
# Python 3 implementation
# of above approach
# Function to check whether
# it is possible to make a
# regular polygon with a
# given angle.
def makePolygon(a) :
# N denotes the number of sides
# of polygons possible
n = 360 / (180 - a)
if n == int(n) :
print("YES")
else :
print("NO")
# Driver Code
if __name__ == "__main__" :
a = 90
# function calling
makePolygon(a)
# This code is contributed
# by ANKITRAI1
C#
// C# implementation of
// above approach
using System;
class GFG
{
// Function to check whether
// it is possible to make a
// regular polygon with a
// given angle.
static void makePolygon(double a)
{
// N denotes the number of
// sides of polygons possible
double n = 360 / (180 - a);
if (n == (int)n)
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
// Driver code
static void Main()
{
double a = 90;
// function to print
// the required answer
makePolygon(a);
}
}
// This code is contributed by mits
PHP
<?php
// PHP implementation of above approach
// Function to check whether it
// is possible to make a regular
// polygon with a given angle.
function makePolygon($a)
{
// N denotes the number of
// sides of polygons possible
$n = 360 / (180 - $a);
if ($n == (int)$n)
echo "YES";
else
echo "NO";
}
// Driver code
$a = 90;
// function to print the
// required answer
makePolygon($a);
// This code is contributed
// by ChitraNayal
?>
JavaScript
<script>
// JavaScript implementation of above approach
// Function to check whether it is possible
// to make a regular polygon with a given angle.
function makePolygon(a)
{
// N denotes the number of sides
// of polygons possible
var n = parseFloat(360 / (180 - a));
if (n === parseInt(n))
document.write("YES");
else
document.write("NO");
}
// Driver code
var a = 90;
// function to print the required answer
makePolygon(a);
</script>
Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Similar Reads
Check if it is possible to create a polygon with given n sides Given an array arr[] that contains the lengths of n sides that may or may not form a polygon. The task is to determine whether it is possible to form a polygon with all the given sides. Print Yes if possible else print No.Examples: Input: arr[] = {2, 3, 4} Output: YesInput: arr[] = {3, 4, 9, 2} Outp
5 min read
Check if an N-sided Polygon is possible from N given angles Given an array arr[] of N elements, where each element represents an angle(in degrees) of a polygon, the task is to check whether it is possible to make an N-sided polygon with all the given angles or not. If it is possible then print Yes else print No. Examples: Input: N = 3, arr[] = {60, 60, 60}Ou
4 min read
Check if given polygon is a convex polygon or not Given a 2D array point[][] with each row of the form {X, Y}, representing the co-ordinates of a polygon in either clockwise or counterclockwise sequence, the task is to check if the polygon is a convex polygon or not. If found to be true, then print "Yes" . Otherwise, print "No".In a convex polygon,
9 min read
Angle between 3 given vertices in a n-sided regular polygon Given a n-sided regular polygon and three vertices a1, a2 and a3, the task is to find the angle suspended at vertex a1 by vertex a2 and vertex a3. Examples: Input: n = 6, a1 = 1, a2 = 2, a3 = 4 Output: 90 Input: n = 5, a1 = 1, a2 = 2, a3 = 5 Output: 36 Approach: The angle subtended by an edge on the
6 min read
Minimum area of a Polygon with three points given Given three points of a regular polygon(n > 3), find the minimum area of a regular polygon (all sides same) possible with the points given.Examples: Input : 0.00 0.00 1.00 1.00 0.00 1.00 Output : 1.00 By taking point (1.00, 0.00) square is formed of side 1.0 so area = 1.00 . One thing to note in
13 min read