Check if it is possible to create a polygon with a given angle

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++



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

YES


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.