Java Program to check Triangle is valid using sides

Write a Java Program to check the triangle is valid or not using its sides with an example. Any triangle is valid if the sum of any two sides is greater than the third side. This Java example allows entering three sides of a triangle. Next, we used if-else to check whether the sum of all combinations of two sides is greater than the third side or not. If the condition is True, it is a valid triangle; otherwise, not.

package Area;

import java.util.Scanner;

public class ValidateTriangleusingSides1 {
	private static Scanner sc;

	public static void main(String[] args) {
		double side1, side2, side3;
		sc = new Scanner(System.in);		
		System.out.println("Please Enter Three sides of Triangle ");
		side1 = sc.nextDouble();
		side2 = sc.nextDouble();
		side3 = sc.nextDouble();
		
		if((side1 + side2  >  side3) && 
				(side2 + side3 > side1) &&
				(side1 + side3 > side2)) {
			System.out.println("It is a Valid Triangle");
		}	else 	{
			System.out.println("It is Not a Valid Triangle");
		}
	}
}
Java Program to check Triangle is Valid using Sides 1

Let me enter wrong sides.

Please Enter Three sides of Triangle 
10
90
25
It is Not a Valid Triangle

In this Java program, instead of checking multiple conditions to check triangle is valid or not, we used multiple if or nested if to check sides.

package Area;

import java.util.Scanner;

public class ValidateTriangleusingSides2 {
	private static Scanner sc;

	public static void main(String[] args) {
		double side1, side2, side3;
		int flag = 0;
		sc = new Scanner(System.in);
		
		System.out.println("Please Enter Three sides of Triangle ");
		side1 = sc.nextDouble();
		side2 = sc.nextDouble();
		side3 = sc.nextDouble();
		
		if(side1 + side2  >  side3) {
			if (side2 + side3 > side1) {
				if (side1 + side3 > side2) {
					flag = 1;
				}
			}
		}
		if(flag == 1)
		{
			System.out.println("It is a Valid Triangle");
		}
		else 
		{
			System.out.println("It is Not a Valid Triangle");
		}
	}
}
Please Enter Three sides of Triangle 
70
90
15
It is Not a Valid Triangle

Let me try other values.

Please Enter Three sides of Triangle 
10
20
15
It is a Valid Triangle