Check if Triangle is Valid or Not in Java Programming
Here’s a Code to Check if Triangle is Valid or Not in Java Programming Language. This is a simple C code that takes Triangle Sides as the Input from the User and compares all the Sides. The Code then Checks the Validity of the Triangle with If – Else Block Structure.
A Triangle is Valid only if one of the three sides of the triangle is greater than the other two sides. The Triangle Validity Test will find if the Triangle is valid or not which includes checking the following condition checking:
- First Side + Third Side > Second Side
- Third Side + Second Side > First Side
- Second Side + First Side > Third Side
Also Read: Java Program To Convert Numbers To Words
Method 1: Java Program To Check The Validity Of A Triangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import java.io.*; import java.util.*; class triangle_validity { public static void main(String args[]) { float l1, l2, l3; Scanner sc = new Scanner(System.in); System.out.println("\nLength of First Side: \t"); l1 = sc.nextFloat(); System.out.println("\nLength of Second Side: \t"); l2 = sc.nextFloat(); System.out.println("\nLength of Third Side: \t"); l3 = sc.nextFloat(); if ((l1 + l2) > l3) { if((l1 + l3) > l2) { if(l2 + l3 > l1) { System.out.println("\nThe Triangle is a Valid Triangle\n\n"); } else { System.out.println("\nThe Triangle is Not a Valid Triangle\n\n"); } } else { System.out.println("\nThe Triangle is Not a Valid Triangle\n\n"); } } else { System.out.println("\nThe Triangle is Not a Valid Triangle\n\n"); } } } |
Method 2: Check If Triangle Is Valid Or Not In Java Programming
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import java.io.*; import java.util.*; import java.io.*; import java.util.*; class triangle_validity { int l1, l2,l3; public triangle_validity(int a, int b, int c) { l1 = a; l2 = b; l3 = c; } public void check_validity() { if ((l1 + l2) > l3) { if((l1 + l3) > l2) { if(l2 + l3 > l1) { System.out.println("\nThe Triangle is a Valid Triangle\n\n"); } else { System.out.println("\nThe Triangle is Not a Valid Triangle\n\n"); } } else { System.out.println("\nThe Triangle is Not a Valid Triangle\n\n"); } } else { System.out.println("\nThe Triangle is Not a Valid Triangle\n\n"); } } } class Demo { public static void main(String args[]) { int a, b, c; Scanner sc = new Scanner(System.in); System.out.println("\nLength of First Side: \t"); a = sc.nextInt(); System.out.println("\nLength of Second Side: \t"); b = sc.nextInt(); System.out.println("\nLength of Third Side: \t"); c = sc.nextInt(); triangle_validity obj1 = new triangle_validity(a,b,c); obj1.check_validity(); } } |
Output

Also Read: Java Code To Draw A Circle using AWT and Graphics
If you have any compilation erros or about in this Code To Check if Triangle is Valid or Not in Java Programming, you can write about it in the Comment Section below.