
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Check whether triangle is valid or not if sides are given in Python
- Check whether right angled triangle is valid or not for large sides in Python
- How to check if an URL is valid or not using Java?
- C Program to check if a date is valid or not
- How To Check If The Email Address Is Valid Or Not In Excel?
- Program to check whether given list is in valid state or not in Python
- How to check if a string is a valid keyword in Java?
- Check if a king can move a valid move or not when N nights are there in a modified chessboard in Python
- Check if a king can move a valid move or not when N nights are there in a modified chessboard in C++
- How to check if a given word is a Python keyword or not?
- Check if a Java ArrayList contains a given item or not
- Check if a given graph is tree or not
- How to check if a file exists or not in Java?
- Check whether a string is valid JSON or not in Python
- Check if a given string is a valid number in Python
