Java Exercises: Check if three given side lengths can make a triangle or not
Java Basic: Exercise-195 with Solution
Write a Java program to check if three given side lengths (integers) can make a triangle or not.
Pictorial Presentation:
Sample Solution:
Java Code:
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input side1: ");
int s1 = in .nextInt();
System.out.print("Input side2: ");
int s2 = in .nextInt();
System.out.print("Input side3: ");
int s3 = in .nextInt();
System.out.print("Is the said sides form a triangle: " + isValidTriangle(s1, s2, s3));
}
public static boolean isValidTriangle(int a, int b, int c) {
return (a + b > c && b + c > a && c + a > b);
}
}
Sample Output:
Input side1: 5 Input side2: 6 Input side3: 8 Is the said sides form a triangle: true
Flowchart:
Java Code Editor:
Company: LinkedIn
Contribute your code and comments through Disqus.
Previous: Write a Java program to find the all positions of a given number in a given matrix. If the number not found print ("Number not found!").
Next: Write a Java program to create a spiral array of n * n sizes from a given integer n.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
How to sort an ArrayList?
Collections.sort(testList); Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
Ref: https://bit.ly/32urdSe
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework