# Java Program to Find All the Angles of a Given Triangle

In the previous article, we have seenĀ Java Program to Check Whether a Given Point Lies Inside a Triangle or not

In this article we will discuss about how to find all the angles of a given triangle using Java programming language.

## Java Program to Find All the Angles of a Given Triangle

Before jumping into the program directly, let’s first know how can we Find All the Angles of a Given Triangle

Explanation:

To Find All the Angles of a Given Triangle:

• Find out the length of three sides of the triangle from the coordinates.
• Then we will use the formula beta = acos( ( a^2 + b^2 – c^2 ) / (2ab) )
• Convert the obtained angle from radian to degree.

Example:

Let’s see different ways to find all the angles of a given triangle.

### Method-1: Java Program to Find All the Angles of a Given Triangle By Using Static Value

Approach:

1. Declare the value for the three coordinates.
2. Then call the angle() method by passing all 3 coordinates.
3. Then print the result.

Program:

import java.awt.*;
import static java.lang.Math.*;

public class Main
{
public static void main(String[] args)
{
// Static initialization of the side of the triangle
Point a = new Point(0,0);
Point b = new Point(0,1);
Point c = new Point(1,0);
// Calling user-defined function by passing the coordinates as parameter
angle(a, b ,c);
}

// Function to find out the area of the circumcircle
static void angle(Point A, Point B, Point C)
{
// Calculates the square of the sides
int sideSquare1= calcSideSquare(A,B);
int sideSquare2= calcSideSquare(B,C);
int sideSquare3= calcSideSquare(C,A);

// Calculates the length of the sides
float side1= (float)sqrt(sideSquare1);
float side2= (float)sqrt(sideSquare2);
float side3= (float)sqrt(sideSquare3);

// Calculates the angles
float angle1= (float) acos((sideSquare2 + sideSquare3 -sideSquare1)/(2*side2*side3));
float angle2= (float) acos((sideSquare1 + sideSquare3 -sideSquare2)/(2*side1*side3));
float angle3= (float) acos((sideSquare1 + sideSquare2 -sideSquare3)/(2*side1*side2));

// Printing the angles
System.out.println("angle 1 : " + radianToDegree(angle1));
System.out.println("angle 2 : " + radianToDegree(angle2));
System.out.println("angle 3 : " + radianToDegree(angle3));
}

// Returns square of the sides
static int calcSideSquare(Point p1, Point p2)
{
int xVaries = p1.x- p2.x;
int yVaries = p1.y- p2.y;
return xVaries*xVaries + yVaries*yVaries;
}

{
float  pi = (float)3.14;
float degree = rad * 180 / pi;
return degree;
}
}

Output:

angle 1 : 45.022823
angle 2 : 90.04565
angle 3 : 45.022823

### Method-2: Java Program to Find All the Angles of a Given Triangle By Using User Input Value

Approach:

1. Take user input the value for the three coordinates.
2. Then call the angle() method by passing all 3 coordinates.
3. Then print the result.

Program:

import java.awt.*;
import static java.lang.Math.PI;
import static java.lang.Math.sqrt;
import static java.lang.Math.acos;
import java.util.*;

public class Main
{
public static void main(String[] args)
{
//Scanner class object created
Scanner scan = new Scanner(System.in);
// Taking user input of the coordinates of the triangle
System.out.println("Enter First Point Coordinates");
Point a = new Point(scan.nextInt(),scan.nextInt());
System.out.println("Enter Second Point Coordinates");
Point b = new Point(scan.nextInt(),scan.nextInt());
System.out.println("Enter Third Point Coordinates");
Point c = new Point(scan.nextInt(),scan.nextInt());

// Calling user-defined function by passing the coordinates as parameter
angle(a, b ,c);
}

// Function to find out the area of the circumcircle
static void angle(Point A, Point B, Point C)
{
// Calculates the square of the sides
int sideSquare1= calcSideSquare(A,B);
int sideSquare2= calcSideSquare(B,C);
int sideSquare3= calcSideSquare(C,A);

// Calculates the length of the sides
float side1= (float)sqrt(sideSquare1);
float side2= (float)sqrt(sideSquare2);
float side3= (float)sqrt(sideSquare3);

// Calculates the angles
float angle1= (float) acos((sideSquare2 + sideSquare3 -sideSquare1)/(2*side2*side3));
float angle2= (float) acos((sideSquare1 + sideSquare3 -sideSquare2)/(2*side1*side3));
float angle3= (float) acos((sideSquare1 + sideSquare2 -sideSquare3)/(2*side1*side2));

// Printing the angles
System.out.println("angle 1 : " + radianToDegree(angle1));
System.out.println("angle 2 : " + radianToDegree(angle2));
System.out.println("angle 3 : " + radianToDegree(angle3));
}

// Returns square of the sides
static int calcSideSquare(Point p1, Point p2)
{
int xVaries = p1.x- p2.x;
int yVaries = p1.y- p2.y;
return xVaries*xVaries + yVaries*yVaries;
}

{
float  pi = (float)3.14;
float degree = rad * 180 / pi;
return degree;
}
}

Output:

Enter First Point Coordinates
0 0
Enter Second Point Coordinates
1 3
Enter Third Point Coordinates
2 0
angle 1 : 71.60135
angle 2 : 71.60135
angle 3 : 36.8886

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Articles: