Introduction

   Understanding the properties of triangles is fundamental in geometry. Triangles can be classified based on the lengths of their sides, and knowing how to check whether a triangle is equilateral, isosceles, or scalene is a useful skill. Additionally, calculating the area of a triangle based on its sides is a common problem in mathematics and programming.

In this blog post, we’ll create a Java program to determine whether a triangle is equilateral, isosceles, or scalene and calculate its area.

Triangle Types

  • Equilateral Triangle: All three sides are equal.
  • Isosceles Triangle: Two sides are equal.
  • Scalene Triangle: All sides are of different lengths.

Program Explanation

Here's a Java program that reads three side lengths, checks whether they form a triangle, classifies the triangle, and calculates its area.

Code

    
        
        import java.io.*;

        class triangle {
            public static void main(String args[]) throws IOException {
                double a, b, c, s;
                System.out.println("Enter the 3 values to be checked:");
                DataInputStream si = new DataInputStream(System.in);
        
                a = Double.parseDouble(si.readLine());
                b = Double.parseDouble(si.readLine());
                c = Double.parseDouble(si.readLine());
        
                // Check if the sides can form a triangle
                if ((a + b) > c && (a + c) > b && (b + c) > a) {
                    System.out.println("The sides form a triangle");
        
                    // Determine the type of triangle
                    if (a == b && b == c) {
                        System.out.println("This is an equilateral triangle");
                    } else if (a == b || a == c || b == c) {
                        System.out.println("This is an isosceles triangle");
                    } else {
                        System.out.println("This is a scalene triangle");
                    }
        
                    // Calculate the semi-perimeter
                    s = (a + b + c) / 2;
                    System.out.println("s = " + s);
        
                    // Calculate the area using Heron's formula
                    double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
                    System.out.println("Area = " + area);
                } else {
                    System.out.println("The given sides can't form a triangle");
                }
            }
        }            
        
        


Output


        Enter the 3 values to be checked:
        20
        20
        20    
        The sides form a triangle
        This is an equilateral triangle
        s = 30.0
        Area = 173.20508075688772
        


 

Explanation

1. Input Handling:
The program prompts the user to input three side lengths.
2. Checking for a Valid Triangle:
The program checks whether the three sides can form a triangle using the triangle inequality theorem:
The sum of the lengths of any two sides must be greater than the length of the remaining side.
3. Classifying the Triangle
  • Equilateral: All three sides are equal.
  • Isosceles: Any two sides are equal.
  • Scalene: All three sides are different.
4.Calculating the Area
5. Displaying the Result

Conclusion

     This Java program efficiently checks whether three sides can form a triangle and then classifies the triangle as equilateral, isosceles, or scalene. Additionally, the program calculates the area of the triangle using Heron’s formula, a classic method for finding the area of a triangle when all three sides are known.

This code is a useful tool for students and professionals dealing with geometric problems, providing a quick and accurate way to assess triangles and their properties.

Happy coding!