How to Determine if three lengths form a Triangle

We check if a triangle is valid or not using using Sides . Any triangle is valid if the sum of the two sides of a triangle is greater than the third side. We write java program using this approach .

How to determine if three lengths form a triangle

Let’s A , B ,C are three sides of triangle then it form a triangle if

  1. A+B > C
  2. B+C > A
  3. C+A > B

We write java program to check if a triangle is valid or not when sides are given .Now we will determine if three lengths form a triangle using java program .

import java.util.Scanner;

public class TriangleValid {
   public static void main(String[] args) {
	
	   int side1 , side2, side3;
	   //create scanner object to get input from user  
       Scanner sc=new Scanner(System.in);  
         
       System.out.println("Enter side1 ");  
         
       //take input from user   
       side1 = sc.nextInt(); 
       
       System.out.println("Enter side2 ");  
       
       //take input from user   
       side2 = sc.nextInt(); 
       
       System.out.println("Enter side3 ");  
       
       //take input from user   
       side3 = sc.nextInt(); 
       
       if( (side1 + side2 > side3) && (side2 + side3 > side1) && (side1 + side3 > side2) ){
    	   System.out.println("This is valid triangle");
       }else {
    	   System.out.println("This is not valid triangle");
	}
	   
     }
}

Output:

Enter side1 
10
Enter side2 
5
Enter side3 
7
This is valid triangle

In this tutorial we learned how to determine if three lengths form a triangle using java .You can learn more java program for practice .

Leave a Reply

Your email address will not be published. Required fields are marked *

1 + 3 =