Java Program to Find Number of Angles in N Sided Convex Polygon

In the previous article, we have discussed about Java Program to Find Centroid of a Triangle

In this article we are going to see how to find number of angles in N sided convex polygon using Java programming language.

Java Program to Find Number of Angles in N Sided Convex Polygon

Before Jumping into the program directly let’s see how to find number of angles in N sided convex Polygon.

Explanation:

Suppose there is a n-sided convex polygon

Where n >= 3

Now, we need to find the no. Of diagonals present in that polygon

As We know,

No. Of diagonals = n*(n-3)/2

Example:

n = 7

Diagonals = n*(n-3)/2 = 14

Let’s see different ways to find number of angles in N sided convex polygon.

Method-1: Java Program to Find Number of Angles in N Sided Convex Polygon By Using Static Value

Approach:

  • Declare a int variable say ‘n’ and assign the value to it, which holds the the no. of sides of a polygon.
  • Now, find the no. of diagonals using the formula n*(n-3)/2
  • Print the result.

Program:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
    int n = 7;
    // formula to find the no. Of diagonals present in the polygon
    int diagonal =  n*(n-3)/2;	  
    System.out.println("The no. Of diagonals present in the polygon is " + diagonal);
   }
}
Output:

The no. Of diagonals present in the polygon is 14

Method-2: Java Program to Find Number of Angles in N Sided Convex Polygon By Using User Input Value

Approach:

  • Declare a int variable say ‘n’ and take the value as user input, which is the no. of sides of a polygon.
  • Now, find the no. of diagonals using the formula n*(n-3)/2
  • Print the result.

Program:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
    //Scanner class object created
    Scanner s = new Scanner(System.in);
    //Taking input of number of sides of a polygon
    System.out.println("Enter the no. Of sides of a polygon");
    int n = s.nextInt();

    // formula to find the no. Of diagonals present in the polygon
    int diagonal =  n*(n-3)/2;	  
    System.out.println("The no. Of diagonals present in the polygon is " + diagonal);
   }
}
Output:

Enter the no. Of sides of a polygon
5
The no. Of diagonals present in the polygon is 5

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: