Write a program to input all sides of a triangle and check whether triangle is valid or not


The program first prompts the user to enter three sides of a triangle using the Scanner class. Then it uses nested if statements to check if the sum of any two sides is greater than the third side. If all three conditions are true, then the program prints "Triangle is valid". Otherwise, it prints "Triangle is not valid".

This program provides a more comprehensive validation for checking if three sides could form a valid triangle or not, as it checks for the triangle inequality theorem, which states that the sum of the lengths of any two sides of a triangle must be greater than the length of the third side. However, it is important to note that this validation does not guarantee that the angles of the triangle would be valid or not, as there are other conditions to be met in order for a triangle to be valid.

Source Code

import java.util.Scanner;
class All_Side_Triangle
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.println("Enter Three Sides of Triangle :");
		int side1 = input.nextInt();
		int side2 = input.nextInt();
		int side3 = input.nextInt();
		if((side1 + side2) > side3)
			if((side2 + side3) > side1)
				if((side1 + side3) > side2) 
					System.out.println("Triangle is valid.");
				else
					System.out.println("Triangle is not valid.");
			else
				System.out.println("Triangle is not valid.");
		else
			System.out.println("Triangle is not valid.");
	}
}

Output

Enter Three Sides of Triangle :
60
60
60
Triangle is valid.

Example Programs