Triangle Validity Check
Check if three sides can form a valid triangle using triangle inequality.
BeginnerTopic: Module 2: Conditional Programs
Java Triangle Validity Check Program
This program helps you to learn the fundamental structure and syntax of Java programming.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter side a: ");
int a = sc.nextInt();
System.out.print("Enter side b: ");
int b = sc.nextInt();
System.out.print("Enter side c: ");
int c = sc.nextInt();
if (a + b > c && a + c > b && b + c > a) {
System.out.println("Triangle is Valid");
} else {
System.out.println("Triangle is Not Valid");
}
sc.close();
}
}Output
Enter side a: 3 Enter side b: 4 Enter side c: 5 Triangle is Valid
Understanding Triangle Validity Check
We use the triangle inequality theorem: sum of any two sides must be greater than the third side.
Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.