Java Program to Check if Triangle is Valid or Not if Sides are Given

In the previous article, we have seenĀ Java Program to Find Area of Circumcircle of an Equilateral Triangle

In this article we will discuss about how to check if triangle is valid or not if sides are given using Java programming language.

Java Program to Check if Triangle is Valid or Not if Sides are Given

Before jumping into the program directly, let’s first know how we can check if triangle is valid or not if sides are given

Explanation:

Formula to Check if Triangle is Valid or Not if Sides are Given :
For a triangle with sides A,B AND C, no side should be greater than sum of the other two 
i.e.
A<B+C 
B<C+A  
C<A+B

Example:

When A = 7, B = 10 and C = 5
7<10+5
10<7+5
5<10+7
Hence a triangle with these sides is valid.

Let’s see different ways to Check if Triangle is Valid or Not if Sides are Given.

Method-1: Java Program to Check if Triangle is Valid or Not if Sides are Given By Using Static Input Values

Approach:

  1. Declare the value for three sides.
  2. Then call the sideCheck() method by passing three sides values as parameter.
  3. In this method each side is compared with the sum of the other two.
  4. Then print the result.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        // Static values for the sides of the triangle   
        int side1 = 14, side2 = 20, side3 = 10;
        if(sideCheck(side1,side2,side3))
            System.out.println("The triangle is valid");
        else
            System.out.println("The triangle is invalid");
    }

    // Checks all three conditions for the triangle to be valid and returns true if passed
    static boolean sideCheck(int a, int b, int c)
    {
        // Checks if 1st side is greater than or equals to sum of the other two sides and returns false
        if(a>=(b+c))
            return false;
            // Checks if 2nd side is greater than or equals to sum of the other two sides and returns false
        if(b>=(a+c))
            return false;
            // Checks if 3rd side is greater than or equals to sum of the other two sides and returns false
        if(c>=(b+a))
            return false;
        return true;
    }
}

Method-2: Java Program to Check if Triangle is Valid or Not if Sides are Given By Using User Input Values

Approach:

  1. Take user input the three sides of the triangle.
  2. Then call the sideCheck() method by passing three sides values as parameter.
  3. In this method each side is compared with the sum of the other two.
  4. Then print the result.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        // Takes threee sides as input from the user
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the three sides of the triangle");
        int side1 = scan.nextInt(), side2 = scan.nextInt(), side3 = scan.nextInt();
        if(sideCheck(side1,side2,side3))
            System.out.println("The triangle is valid");
        else
            System.out.println("The triangle is invalid");
    }

    // Checks all three conditions for the triangle to be valid and returns true if passed
    static boolean sideCheck(int a, int b, int c)
    {
        // Checks if 1st side is greater than or equals to sum of the other two sides and returns false
        if(a>=(b+c))
            return false;
            // Checks if 2nd side is greater than or equals to sum of the other two sides and returns false
        if(b>=(a+c))
            return false;
            // Checks if 3rd side is greater than or equals to sum of the other two sides and returns false
        if(c>=(b+a))
            return false;
        return true;
    }
}
Case-1
Enter the three sides of the triangle
3
5
4
The triangle is valid

Case-2
Enter the three sides of the triangle
9
4
3
The triangle is invalid

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Articles: