How to Know If Two Convex Regular Polygons have Same Centre or Not in Java?


A polygon is a 2-dimensional closed shape that has at least 3 sides. Depending on the number of sides, relationships of the sides and angles, and other characteristics, polygons can be classified under different names like triangles, squares, and quadrilaterals.

The convex polygon definition explains that one is a polygon whose angles are all less than 180 degrees each. That also means that the vertices, the point where two sides meet, all point outward from the centre of the shape.

In this article, we will find if two convex regular polygons have same centre or not.

We will take two sides of the convex regular polygon with sides “a” and “b” where “b>a”. Then we need to check whether polygons have the same center or not if “a” sided polygon is inscribed in an “b” sided polygon.

We will find this with the formula “b % a == 0” which denotes sides of “a” sides polygon equally covers “b” sided polygon which says that both the polygon have equal centre.

Let’s start!

To show you some instances

Instance-1

  • Suppose the value of a = 15 and b = 30.

  • After putting the values in formula “b % a == 0” the result will be −

    • Polygon have same centre

Instance-2

  • Suppose the value of a = 7 and b = 22.

  • After putting the values in formula “b % a == 0” the result will be −

    • Polygon do not have same centre

Algorithm

  • Step-1 − Declare and initialize the variables.

  • Step-2 − Check for the condition of centre i.e “b % a == 0”.

  • Step-3 − Print the result.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Inputs

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input

In this approach, value of “a” and “b” will be assigned. Then as per the algorithm we will find if two convex regular polygons have same centre or not

Example

public class Main {
   //main method
   public static void main(String[] args){
      //declaring variables
      int a = 7;
      int b = 22;
      //checking for condition of center
      if (b % a == 0){
         //print if polygon have same center
         System.out.print("Polygon have same center");
      } else {
         //print if polygon do not have same center
         System.out.print("Polygon do not have same center");
      }
   }
} 

Output

Polygon do not have same center

Approach-2: By Using User Defined Method

In this approach, value of “a” and “b” will be assigned. Then call a user defined method by passing the given values and as per the algorithm we will find if two convex regular polygons have same centre or not.

Example

public class Main {
   //main method
   public static void main(String[] args){
      
      //declaring variables
      int a = 15;
      int b = 30;
      
      //calling user defined method
      func(a, b);
   }

   //user defined method
   static void func(int a, int b){
   
      //checking for condition of center
      if (b % a == 0){
         
         //print if polygon have same center
         System.out.print("Polygon have same center");
      } else {
         //print if polygon do not have same center
         System.out.print("Polygon do not have same center");
      }
   }
} 

Output

Polygon have same center

In this article, we explored different approaches to check if two convex regular polygons have same centre or not by using Java programming language.

Updated on: 04-May-2023

109 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements