Java Program to Find the Area of a Triangle

Introduction

This tutorial shows how to find the area of a triangle in Java. It starts with an overview of the formulas used for computing the area of triangles, After the formulas, it gives the program for the base and the height, a program for three sides with Heron's formula, and one for an equilateral triangle.

The formulas for finding area of a triangle

There are multiple formulas for finding the area of a triangle, and choice of formula to use depends on what is known about the triangle. Let's take a lot at the 3 common triangle area formulas -
  1. When triangle base and height are provided: area = 1/2 x base x height, where the height is the distance from the base to the opposite corner, measured at a right angle.
  2. When the length of three sides of a triangle are provided(Heron's formula): Given the 3 sides' lengths to be a, b and c -
    • half perimeter(s) = (a + b + c) / 2
    • area = the square root of s x (s - a) x (s - b) x (s - c)
  3. For an equilateral triangle: Given each side to be of length a -
    • area = the square root of 3 / 4 x a x a

Formula 1 - when triangle base and height are known

Java program to find the area of a triangle from the base and the height
//AreaOfTriangle.java
package com.javabrahman.generaljava;

import java.util.Scanner;

public class AreaOfTriangle {

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the base and the height: ");
    double base = scanner.nextDouble();
    double height = scanner.nextDouble();

    if (base < 0 || height < 0) {
      System.out.println("The base and the height cannot be negative");
    } else {
      double area = 0.5 * base * height;
      System.out.printf("Area of the triangle: %.2f%n", area);
    }
  }
}
 OUTPUT for different input values
Enter the base and the height: 10 5
Area of the triangle: 25.00

Enter the base and the height: 3.5 4
Area of the triangle: 7.00

Enter the base and the height: 0 7
Area of the triangle: 0.00

Enter the base and the height: -2 6
The base and the height cannot be negative
Explanation of the code
  • The base and the height are double values, so decimals such as 3.5 work. They can be typed on one line, separated by a space.
  • The area is 0.5 * base * height. The factor is written as 0.5, not 1 / 2, because 1 / 2 is calculated with whole numbers and gives 0.
  • Lengths cannot be negative, so such input is rejected. A base or a height of 0 gives the area 0.
  • The area for a base of 10 and a height of 5 is 25.00. The method printf with %.2f writes two digits after the decimal point.

Formula 2 - Heron's formula - when length of all 3 sides of triangle are known

When the three sides of a triangle are known, Heron's formula can be used to calculate the area.

An important charecteristic of triangles is that each side of a triangle has to be shorter than the sum of the other two - otherwise the sides cannot be joined into a closed shape. This condition is used to check the input side lengths in the program that follows -
Area of a triangle from three sides with Heron's formula
//AreaOfTriangleHeron.java
package com.javabrahman.generaljava;

import java.util.Scanner;

public class AreaOfTriangleHeron {

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the three sides: ");
    double a = scanner.nextDouble();
    double b = scanner.nextDouble();
    double c = scanner.nextDouble();

    boolean isTriangle = a > 0 && b > 0 && c > 0 && a + b > c && a + c > b && b + c > a;
    if (!isTriangle) {
      System.out.println("These lengths cannot form a triangle");
    } else {
      double s = (a + b + c) / 2;                           // half the perimeter
      double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
      System.out.printf("Area of the triangle: %.2f%n", area);
    }
  }
}
 OUTPUT for different input values
Enter the three sides: 3 4 5
Area of the triangle: 6.00

Enter the three sides: 5 5 5
Area of the triangle: 10.83

Enter the three sides: 7 8 9
Area of the triangle: 26.83

Enter the three sides: 1 2 10
These lengths cannot form a triangle
Explanation of the code
  • The three sides are read as double values with a Scanner.
  • The boolean isTriangle is true only when every side is positive and each side is shorter than the sum of the other two. If it is false, the program prints a message and calculates nothing.
  • For a real triangle, s is half the perimeter, and the area is the square root of s * (s - a) * (s - b) * (s - c). Math.sqrt()Click to read: Math.sqrt() gives the square root, and printf prints it with two decimal places.
  • The triangle 3, 4, 5 has the area 6.00, as calculated above. The equilateral triangle 5, 5, 5 has the area 10.83. For 1, 2 and 10, the sum 1 + 2 is less than 10, so the sides cannot meet, and the program says so.

Formula 3 - for an equilateral triangle

An equilateral triangle has same length for all three sides. Let's see a program that shows how to determine the area of an equilateral triangle in Java -
Area of an equilateral triangle
//AreaOfEquilateralTriangle.java
package com.javabrahman.generaljava;

import java.util.Scanner;

public class AreaOfEquilateralTriangle {

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the side: ");
    double side = scanner.nextDouble();

    double area = Math.sqrt(3) / 4 * side * side;
    System.out.printf("Area of the equilateral triangle: %.2f%n", area);
  }
}
 OUTPUT for different input values
Enter the side: 5
Area of the equilateral triangle: 10.83

Enter the side: 6
Area of the equilateral triangle: 15.59
Explanation of the code
  • The side is read as a double with a Scanner; one value is enough, because all three sides are equal.
  • The area is Math.sqrt(3) / 4 * side * side, the formula for an equilateral triangle, and printf prints it with two decimal places.
  • For a side of 5, the result 10.83 is the same as the one Heron's formula gave for 5, 5, 5.

Summary

In this tutorial we looked at Java programs for determining the area of a triangle based on 3 main formulas, viz. finding area from base and height, from three sides' lengths with Heron's formula, and for an equilateral triangle, each with its output and a detailed explanation of the code.