Check If a Triangle Is Valid Based on Given Sides in Java



A Triangle is a polygon that has 3 sides, and it consists of three sides and three vertices. The sum of the three internal angles is up to 180 degrees.

Below is the diagram of a triangle having three sides (a, b, and c):

triangle

In a valid triangle, if you add any two sides, then it will be greater than the third side. As per our problem statement, we have to check if a triangle is valid or not, if three sides are given, using the Java programming language.

Suppose a, b and c are the three sides of the triangle, we need to check whether the following three conditions are satisfied. If the conditions are satisfied, then the triangle will be considered valid; if not, it will be invalid.

a + b > c 
b + c > a
c + a > b

Input & Output Scenarios

Following are the scenarios to check if a Triangle is valid or not:

Scenario 1:

Suppose we have the sides of the Triangle:

Input: a = 8, b = 9, c = 5
Output:
a + b = 8 + 9 = 17 > c = 5, 
b + c = 9 + 5 = 14 > a = 8,
c + a = 5 + 8 = 13 > b = 9

All the conditions are satisfied, so the triangle is valid with the given sides.

Scenario 2:

Suppose we have the sides of the Triangle:

Input: a = 1, b = 4, c = 7
Output:
a + b = 1 + 4 = 05 < c = 7,
b + c = 4 + 7 = 11 > a = 1,
c + a = 7 + 1 = 08 > b = 4

Since the condition a + b > c is not satisfied, the triangle with the given sides is not valid.

Let's implement the above logic in a Java program to check whether a Triangle with the given sides is valid or not.

Example 1

If the above condition is satisfied, the Triangle is valid with given sides.

In the following example, we check whether the Triangle is valid with the given sides a = 4, b = 6, and 8 = 0. We compare the a + b > c, a + c > b, and b + c > a, and if this condition is satisfied, the Triangle is valid:

public class checkValidTriangle {

   public static void main(String args[]) {  
      
      //the sides of the triangle
      double a = 4;
      double b = 6;
      double c = 8;
      
      System.out.println("The given sides of the Triangle are: ");
      System.out.println("a = " + a + ", b = " + b + ", c = " + c);
      	  
      //checking if triangle is valid or not by using the logic
      if((a + b > c && a + c > b && b + c > a)){
         System.out.println("Yes! Triangle is valid with given sides.");
      }  else {
         System.out.println("No! Triangle is not valid with the given sides.");
      }
   }
}

The above program produces the following output:

The given sides of the Triangle are: 
a = 4.0, b = 6.0, c = 8.0
Yes! Triangle is valid with given sides.

Example 2

If the above condition is not satisfied, the Triangle is invalid with the given sides.

Here is another example of checking if a Triangle is valid with the given sides a = 5, b = 9, and c = 3. We define a method named isValidTriangle(a, b, c), which accepts three parameters and uses the same logic to compare the condition and returns True or False based on the condition satisfied:

public class checkValidTriangle {

   //define a method to check valid Triangle
   public static boolean isValidTriangle(double a, double b, double c){
      if(a + b > c && b + c > a && a + c > b){
         return true;
      }
      return false;
   }
   public static void main(String args[]) {  
   
      //the sides of the triangle
      double a = 1;
      double b = 4;
      double c = 7;
      
      System.out.println("The given sides of the Triangle are: ");
      System.out.println("a = " + a + ", b = " + b + ", c = " + c);
      
      //calling the method to check valid Triangle
      System.out.println("Is a Triangle with given sides valid? " + isValidTriangle(a, b, c));
   }
}

Below is the output of the above program:

The given sides of the Triangle are: 
a = 1.0, b = 4.0, c = 7.0
Is a Triangle with given sides valid? false
Updated on: 2025-05-27T13:37:32+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements