Java Program to Check if Two Convex Regular Polygon Have Same Center or Not

In the previous article, we have discussed about Java Program to Find Number of Angles in N Sided Convex Polygon

In this article we are going to see how to check if two convex regular polygon have same center or not by using Java programming language.

Java Program to Check if Two Convex Regular Polygon Have Same Center or Not

Before Jumping into the program directly let’s see how to Check if Two Convex Regular Polygon Have Same Center or Not.

Explanation:

Lets take 2 polygons having M-sides and N-sides

Now, we need to find if 2 polygons have same center or not

  • If M%N = 0 , then both polygons have same center
  • If M%N != 0 , then both polygons have  not same center

Example:

M = 15
N = 5
Since M%N = 0, hence both polygons have same center.

Let’s see different ways to check if two convex regular polygon have same center.

Method-1: Java Program to Check if Two Convex Regular Polygon Have Same Center or Not By Using Static Value

Approach:

  • Declare an int variable say ‘m’ and assign the value to it, which holds the Of sides of M-sided Polygon
  • Declare an int variable say ‘n’ and assign the value to it, which holds the Of sides of N-sided Polygon
  • Check if they have same center or not by using the formula M%N
  • Print the result.

Program:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
      //number of sides of both polygons are declared
      int m = 15;
      int n = 5;
      //check both polygons are having same center or not
      if(m%n == 0)
        System.out.println("Both the polygons have same center");
      else
        System.out.println("Both the polygons have not same center");
   }
}
Output:

Both the polygons have same center

Method-2: Java Program to Check if Two Convex Regular Polygon Have Same Center or Not By Using User Input Value

Approach:

  • Declare an int variable say ‘m’ which holds the of sides of M-sided Polygon.
  • Declare an int variable say ‘n’ which holds the of sides of N-sided Polygon.
  • Then we will take the value of “m”, “n” as user input using scanner class.
  • Check if they have same center or not by using the formula M%N
  • Print the result.

Program:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
      Scanner s = new Scanner(System.in);
      System.out.println("Enter the no. of sides of a M-sided polygon: ");
      int m = s.nextInt();
      System.out.println("Enter the no. of sides of a N-sided polygon: ");
      int n = s.nextInt();

      //check both polygons are having same center or not
      if(m%n == 0)
        System.out.println("Both the polygons have same center");
      else
        System.out.println("Both the polygons have not same center");
   }
}
Output:

Enter the no. of sides of a M-sided polygon: 
12
Enter the no. of sides of a N-sided polygon: 
8
Both the polygons have not same center

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: