Java Program to find Triangle is Valid using Angles

Write a Java Program to find triangle is valid using angles with an example. Any triangle is valid if the sum of its three angles equals 180. This Java example allows entering three angles of a triangle. Next, we used if-else to check whether the sum of the given angles equals 180. If the condition is True, it is a valid Triangle; otherwise, not.

package Area;

import java.util.Scanner;

public class ValidTriangleusingAngles1 {
	private static Scanner sc;

	public static void main(String[] args) {
		int ang1, ang2, ang3, total;
		sc = new Scanner(System.in);
		
		System.out.println("Enter Triangles First, Second & Thrid Angles = ");
		ang1 = sc.nextInt();
		ang2 = sc.nextInt();
		ang3 = sc.nextInt();

		total = ang1 + ang2 + ang3; 
		
		if (total == 180) {
			System.out.println("It is a Valid Triangle");
		}
		else {
			System.out.println("It is Not a Valid Triangle");
		}
	}
}
Enter Triangles First, Second & Thrid Angles = 
60
70
50
It is a Valid Triangle

Let me try wrong values

Enter Triangles First, Second & Thrid Angles = 
70
90
120
It is Not a Valid Triangle

In the above Java example, we forgot to check the other critical condition. We have to check whether any of the triangle angles are zero because if it is the case, the triangle is invalid. In this Java program, we altered the above if else and checked whether the triangle is valid or not using the given angles.

package Area;

import java.util.Scanner;

public class ValidTriangleusingAngles2 {
	private static Scanner sc;

	public static void main(String[] args) {
		int ang1, ang2, ang3, total;
		sc = new Scanner(System.in);
		
		System.out.println("Enter Triangles First, Second & Thrid Angles = ");
		ang1 = sc.nextInt();
		ang2 = sc.nextInt();
		ang3 = sc.nextInt();

		total = ang1 + ang2 + ang3; 
		
		if (total == 180 && ang1 != 0 && ang2 != 0 && ang3 != 0) 
		{
			System.out.println("It is a Valid Triangle");
		}
		else {
			System.out.println("It is Not a Valid Triangle");
		}
	}
}
Java Program to find Triangle is Valid using Angles 1

Let me enter other values

Enter Triangles First, Second & Thrid Angles = 
90
90
0
It is Not a Valid Triangle