Java Program to Find Nth Angle of a Polygon whose Initial Angle and Per Angle Increment is Given

In this article we are going to see how to find Nth angle of a polygon whose initial angle and per angle increment is given by using Java programming language.

Java Program to Find Nth Angle of a Polygon whose Initial Angle and Per Angle Increment is Given

Before Jumping into the program directly let’s see how to find Nth angle of a Polygon whose initial angle and per angle increment is given

Let the number of sides of the polygon is N

Initial angle of the polygon is O

Let the per angle increase be P

nth angle be n

i.e there is a N sides polygon with initial angle O and difference P

nth angle = O + (n-1) * P

Using arithmetic Progression

Total nth angle = (n*((2*O)+(n-1)*P)/2

Now, we know the sum of N sided polygon (say S)  = 180 * (N-2)

Now, if (nth == S) then it is possible

Else not possible

Example:

N = 3
O = 30
P = 30
n = 3
Sum of nth angle =  (n*((2*O)+(n-1)*P)/2 = 180
Sum of angle of N-sides polygon = 180*(N-2) = 180
nth angle = O + (n-1) * P = 90

Let’s see different ways to find Nth angle of a polygon whose initial angle and per angle increment is given.

Method-1: Java Program to Find Nth Angle of a Polygon whose Initial Angle and Per Angle Increment is Given By Using Static Input Value

Approach:

• Declare an int variable say ‘N’ and assign the value to it, which holds the no of sides of the polygon.
• Declare an double variable say ‘O’ and assign the value to it, which holds the initial angle of the polygon.
• Declare an double variable say ‘P’ and assign the value to it, which holds the difference between 2 angle.
• Declare an int variable say ‘n’ and assign the value to it, which holds the nth angle of the polygon
• Find the Total nth angle using the formula (n*((2*O)+(n-1)*P)/2
• Then find the sum of total angle of Nth side using the formula 180*(N-2)
• Now, check if total nth angle <= sum of angle of N sided polygon then print the result else print “Not Possible”.

Program:

import java.io.*;
class Main
{
public static void main(String [] args)
{
int N = 3;
double O = 30;
double P = 30;
int n = 2;
// formula to find nth angle
double nthAngle =  O + (n-1) * P;
// formula to find sum of nth angle
double sumOfnthAngle = n*((2*O)+(n-1)*P)/2;
// formula to find sum of angle of N-sides
double sumOfAngleOfNside =  180*(N-2);
// checking condition
if (sumOfnthAngle <= sumOfAngleOfNside )
System.out.println("The nth angle is " + nthAngle);
else
System.out.println("Not Possible");
}
}
Output:

The nth angle is 60.0

Method-2: Java Program to Find Nth Angle of a Polygon whose Initial Angle and Per Angle Increment is Given By Using User Input Value

Approach:

• Declare an int variable say ‘N’ and take the value as user input, which holds the no of sides of the polygon.
• Declare an double variable say ‘O’ and take the value as user input, which holds the initial angle of the polygon.
• Declare an double variable say ‘P’ and take the value as user input, which holds the difference between 2 angle.
• Declare an int variable say ‘n’ and take the value as user input, which holds the nth angle of the polygon
• Find the Total nth angle using the formula (n*((2*O)+(n-1)*P)/2
• Then find the sum of total angle of Nth side using the formula 180*(N-2)
• Now, check if total nth angle <= sum of angle of N sided polygon then print the result else print “Not Possible”.

Program:

import java.io.*;
import java.util.Scanner;
class Main
{
public static void main(String [] args)
{
// scanner class obj ref
Scanner s = new Scanner(System.in);
System.out.print("Enter the no. Of sides of the polygon ");
// to take user input value
int N = s.nextInt();
System.out.print("Enter the initial angle of the polygon ");
// to take user input value
double O = s.nextDouble();
System.out.print("Enter the difference between 2 angle ");
// to take user input value
double P = s.nextDouble();
System.out.print("Enter n to find its nth angle ");
// to take user input value
int n = s.nextInt();
// formula to find nth angle
double nthAngle =  O + (n-1) * P;
// formula to find sum of nth angle
double sumOfnthAngle = n*((2*O)+(n-1)*P)/2;
// formula to find sum of angle of N-sides
double sumOfAngleOfNside =  180*(N-2);
// checking condition
if (sumOfnthAngle <= sumOfAngleOfNside )
System.out.println("The nth angle is " + nthAngle);
else
System.out.println("Not Possible");

}
}
Output-1:

Enter the no. Of sides of the polygon 3
Enter the initial angle of the polygon 30
Enter the difference between 2 angle 10
Enter n to find its nth angle 2
The nth angle is 40.0

Output-2:

Enter the no. Of sides of the polygon 3
Enter the initial angle of the polygon 60
Enter the difference between 2 angle 10
Enter n to find its nth angle 3
Not Possible

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.