Write a Java Program to input two angles from user and find third angle of the triangle. How to find all angles of a triangle if two angles are given by user using Java programming. Java program to calculate the third angle of a triangle if two angles are given.
Required knowledge
Arithmetic operators, Data types, Basic input/output
Properties of triangle
Sum of angles of a triangle is 180°.
![sum of angles of a triangle,triangle,sum of angles,angles,sum of angles of triangle,triangles,angle sum in a triangle,the sum of angles of a triangle,angles sum of triangle,sum,sum of exterior angles of a triangle,sum of all exterior angles of a triangle,sum of exterior angle of triangle,sum of interior angle of triangle,sum of angles of triangle with practical sum of angles of a triangle,triangle,sum of angles,angles,sum of angles of triangle,triangles,angle sum in a triangle,the sum of angles of a triangle,angles sum of triangle,sum,sum of exterior angles of a triangle,sum of all exterior angles of a triangle,sum of exterior angle of triangle,sum of interior angle of triangle,sum of angles of triangle with practical](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjL0FRBWMcKOjcQ4HGJkgRZ8Lg-3UU0cVRAAMwl72HzUiZmzAZugzs-QAMGTukpX0KmSx9oppS93z_UM35_dIw4S2UPbqrmtzc_Vx5HJePegRnqS0fGfnJ6QrYpqZWMME2MnMwyDkZK3CHH/s1600/triangle.png)
Let us apply basic math to derive formula for third angle. If two angles of a triangle are given, then third angle of triangle is given by -
c=180-(a+b)
Logic to find third angle of a triangle
Step by step descriptive logic to find third angle of a triangle -
Input two angles of triangle from user. Store it in some variable say a and b.
Compute third angle of triangle using formula c = 180 - (a + b).
Print value of third angle i.e. print c.
Program to find angles of a triangle
/** * Java program to find all angles of a triangle if two angles are given */ import java.util.Scanner; class Test { public static void main(String args[]) { int a, b, c; Scanner op=new Scanner(System.in); /* Input two angles of the triangle */ System.out.print("Enter First angles of triangle: "); a=op.nextInt(); System.out.print("Enter Second angles of triangle: "); b=op.nextInt();; /* Compute third angle */ c = 180 - (a + b); /* Print value of the third angle */ System.out.println("Third angle of the triangle = "+c); } }
Output:
Enter First angles of triangle: 70 Enter Second angles of triangle: 80 Third angle of the triangle = 30