w3resource
Java Programming Exercies

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:

Java Basic Exercises: Check if three given side lengths can make a triangle or not

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:

Flowchart: Java exercises: Check if three given side lengths can make a triangle or not

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?



New Content: Composer: Dependency manager for PHP, R Programming


bedava porno