Java Program to Check if Triangle is Valid or Not if Sides are Given
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// Takes threee sides as input from the user
Scanner scan = new Scanner(System.in);
System.out.println("Enter the three sides of the triangle");
int side1 = scan.nextInt(), side2 = scan.nextInt(), side3 = scan.nextInt();
if(sideCheck(side1,side2,side3))
System.out.println("The triangle is valid");
else
System.out.println("The triangle is invalid");
}
// Checks all three conditions for the triangle to be valid and returns true if passed
static boolean sideCheck(int a, int b, int c)
{
// Checks if 1st side is greater than or equals to sum of the other two sides and returns false
if(a>=(b+c))
return false;
// Checks if 2nd side is greater than or equals to sum of the other two sides and returns false
if(b>=(a+c))
return false;
// Checks if 3rd side is greater than or equals to sum of the other two sides and returns false
if(c>=(b+a))
return false;
return true;
}
}
Output:
Enter the three sides of the triangle
3
5
4
The triangle is valid
