How To Check if a Triangle is Valid or Not When Sides are Given in Java?


As we know a triangle is a polygon which has 3 sides. It consists of three sides and three vertices. Three internal angles sum up to 180 degrees.

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 by using Java programming language.

So, we have to check if the three conditions below are satisfied or not. If satisfied then triangle is a valid else triangle is not valid.

Suppose a, b and c are the three sides of the triangle.

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

To show you some instances

Instance-1

If the sides are a=8, b=9, c=5

Then by using the above logic,

a+b=8+9=17 which is greater than c i.e. 5
b+c=9+5=14 which is greater than a i.e. 8
c+a=5+8=13 which is greater than b i.e. 9

Hence, the triangle is valid with given sides.

Instance-2

If the sides are a=7, b=8, c=4

Then by using the above logic,

a+b=7+8=15 which is greater than c i.e. 4
b+c=8+4=12 which is greater than a i.e. 7
c+a=4+7=11 which is greater than b i.e. 8

Hence, the triangle is valid with given sides.

Instance-3

If the sides are a=1, b=4, c=7

Then by using the above logic,

a+b=1+4=5 which is not greater than c i.e. 7
b+c=4+7=11 which is greater than a i.e. 1
c+a=7+1=8 which is greater than b i.e. 4

Hence, the triangle is not valid with given sides. As the condition a+b>c fails.

Algorithm

  • Step 1 − Get the sides of the triangle either by initialization or by user input.

  • Step 2 − Check if it satisfies the condition or not to be a valid triangle.

  • Step 3 − If satisfied then print triangle is valid else not.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Input Value

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using User Input Value

In this approach side lengths values of the triangle will be initialized in the program then by using the algorithm we can check if the triangle is valid or not with given three sides.

Example

public class Main { //main method public static void main(String args[]) { //Declared the side length values of traingle double a = 4; double b = 6; double c = 8; //checking if triangle is valid or not by using the logic if((a + b > c || a + c > b || b + c > a)){ System.out.println("Triangle is Valid"); } else { System.out.println("Triangle is not Valid"); } } }

Output

Triangle is Valid

Approach-3: By Using User Defined

In this approach side lengths values of the triangle will be initialized in the program. Then call a user defined method by passing these sides as parameter and inside method by using the algorithm we can check if the triangle is valid or not with given three sides.

Example

import java.util.*; import java.io.*; public class Main { //main method public static void main(String args[]){ //Declared the side lengths double a = 5; double b = 9; double c = 3; //calling a user defined method to check if triangle is valid or not checkTraingle(a,b,c); } //method to check triangle is valid or not public static void checkTraingle(double a,double b, double 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("Triangle is Valid"); } else { System.out.println("Triangle is not Valid"); } } }

Output

Triangle is Valid

In this article, we explored how to check if a triangle is valid if three sides are given in Java by using different approaches.

Updated on: 27-Oct-2022

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements