Beginners Programs :

Find Missing Third Angle in Triangle : Java Program

Java Program to enter two angles of a triangle and find the third angle in Java, to find the missing third angle, we first need the other two known angles of the triangle. Our expected output is whether the angle is an acute angle, an obtuse angle or a right angle.

To begin the solution, we first require the two angles (a1 and a2). Their values keep varying based on our use case so, we have to read these inputs at runtime. To do so, we can make use of the Scanner class as it can be used to read input of any primitive datatype from the console screen at runtime by the user.

To use this class, we need to first create an object instantiating it and followed by which we invoke our necessary method on the basis of our input type. Here, the two angles of the triangle are of integer type so, we invoke the nextInt() method by using the object created earlier.

Now we have all the necessary inputs, so proceed with further steps of execution. We make a method call for a static method (missingangle) and pass both the input angles (x,y) as arguments of this method.

For knowledge, the sum of all the three angles of a triangle is always 180 degrees. In this static method (missingangle) we first check whether the sum of the two know angles is lesser than or greater than or equal to 90 degrees.

Angle less than 90 degrees is called acute angle, angle greater than 90 degrees is called obtuse angle and angle equal to 90 degrees is called right angle.

If the sum of known angles i.e., x+y is greater than 90 degrees then,

x+y>90 and x+y+missingangle=180 so, missing angle < 90 which means it is an acute angle.

Else, if the sum of known angles is equal to 90 degrees then,

x+y=90 and x+y+missing angle=180 so, missing angle = 90 which means it’s a right angle.

Else, if the sum of known angles is lesser than 90 degrees then,

x+y<90 and x+y+missingangle=180 so, missing angle>90 hence, it’s an obtuse angle.

Using these conditions, we store which type of angle it is in a string variable (str) and return this variable to the main method. This is our resultant output which can then, be displayed on the console screen.

String str=” “;

if(x+y>90)

str=”Acute angle”;

if(x+y==90)

str=”Right angle”;

if(x+y<90)

str=”Obtuse angle”;

return str;

Output:

Angle of A Triangle in Java – Type 2

In this type as well, we make use of the same logic and set of statements as discussed above. The difference is that, in the above type we made use of a static method consisting of the main logic whereas here, we place the entire code in the main method itself.

We make use of static method earlier to make the logic part of code reusable. Instead of rewriting the same statements when in need elsewhere, we can just make a method call.

Apart from that, for a static method, we do no need to create an object instantiating the class too because, static method belongs to the class and not the instance. If we are sure of that, we do not need to reuse this elsewhere in the future we can write the entire code within the main method itself as below.

Output: