ThisCalculator

Everyday

Area of Triangle Java Program: Formula & Code

Updated · Standard formula, shown below

Area 25.00 cm²
Base
10 cm
You entered
Height
5 cm
You entered

Area of Triangle Java Program: Formula & Code

This article explains how to write a Java program that calculates the area of a triangle. You will learn the standard formulas (base-height and Heron’s formula), see complete code examples, and understand common pitfalls. The calculator below computes the area using the base-height method, and the code examples show both approaches.

Formula

The most common formula for the area of a triangle is:

Area = (base × height) / 2

Where:

  • base (b) – the length of one side of the triangle, in any unit (e.g., cm, m, inches).
  • height (h) – the perpendicular distance from that base to the opposite vertex, in the same unit.

The result is in square units (e.g., cm², m², in²). This formula works for any triangle, as long as you know a base and its corresponding height.

Worked Example

Using the calculator defaults: base = 10 cm, height = 5 cm.

  1. Multiply base by height: 10 × 5 = 50
  2. Divide by 2: 50 / 2 = 25

The area is 25 cm².

Java Program Using Base and Height

Here is a simple Java program that reads the base and height from the user and prints the area:

import java.util.Scanner;

public class TriangleArea {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter base: ");
        double base = input.nextDouble();

        System.out.print("Enter height: ");
        double height = input.nextDouble();

        double area = (base * height) / 2;
        System.out.println("Area = " + area);
    }
}

This program uses double to handle decimal values. If you only work with integers, you could use int, but be careful: integer division truncates, so (10 * 5) / 2 works fine, but (7 * 3) / 2 would give 10 instead of 10.5. Always use double for area calculations.

Java Program Using Heron’s Formula

When you know all three side lengths but not the height, use Heron’s formula:

Area = √(s(s – a)(s – b)(s – c))

where s = (a + b + c) / 2 (semi-perimeter).

Here is a Java implementation:

import java.util.Scanner;

public class HeronsFormula {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter side a: ");
        double a = input.nextDouble();
        System.out.print("Enter side b: ");
        double b = input.nextDouble();
        System.out.print("Enter side c: ");
        double c = input.nextDouble();

        double s = (a + b + c) / 2;
        double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));

        System.out.println("Area = " + area);
    }
}

Heron’s formula requires that the three sides actually form a triangle (the sum of any two sides must be greater than the third). If not, s - a could be negative, and Math.sqrt would return NaN (Not a Number).

How to Choose the Right Formula

  • Base and height known – use the simple formula. It is fast and easy.
  • Three sides known – use Heron’s formula. It avoids needing to find the height.
  • Two sides and the included angle known – use Area = (a × b × sin(θ)) / 2. This is less common in basic exercises but useful in trigonometry.

For most school assignments, the base-height method is what you need. For real-world measurements, you often have sides rather than heights, so Heron’s formula becomes handy.

Common Mistakes and Pitfalls

  • Using the wrong height – the height must be perpendicular to the base. Using a slanted side instead gives a wrong area.
  • Integer division – in Java, int / int yields an int. Use double or cast to avoid truncation.
  • Unit mismatch – base and height must be in the same unit. Mixing cm and m leads to nonsense.
  • Forgetting the /2 – the most common error. The area is half the product of base and height.
  • Heron’s formula with invalid sides – always check the triangle inequality first.

Understanding the Output and Precision

The result is in square units. If you input base in cm and height in cm, the area is in cm². For large or small numbers, consider using printf to control decimal places:

System.out.printf("Area = %.2f cm²%n", area);

This prints two decimal places. Floating-point numbers in Java are approximations, so for very precise calculations (like financial or scientific work), you might use BigDecimal, but for a triangle area, double is usually sufficient.

Limitations of the Formulas

The base-height formula is exact for any triangle in Euclidean geometry. Heron’s formula is also exact, but it is computationally more expensive due to the square root. Neither formula works for non-Euclidean geometries (e.g., on a sphere). Also, if you have extremely large or small numbers, floating-point precision can cause tiny errors.

Frequently Asked Questions

How do you calculate the area of a triangle in Java without user input?

You can hardcode the values: double base = 10; double height = 5; double area = (base * height) / 2; and then print the result. This is useful for testing or when the values are fixed.

Can I use integers for base and height?

Yes, but be careful with division. If both are integers, (base * height) / 2 performs integer division, which truncates the decimal part. For example, 7 * 3 / 2 gives 10, not 10.5. To get a precise result, use double or cast one operand to double.

What is the difference between area and perimeter of a triangle?

Area measures the space inside the triangle (square units), while perimeter is the total length of all three sides (linear units). They are different concepts; the perimeter does not determine the area unless you also know the shape.

How do I handle invalid input in the Java program?

Use a try-catch block to catch InputMismatchException when reading numbers with Scanner. For example:

try {
    double base = input.nextDouble();
} catch (InputMismatchException e) {
    System.out.println("Invalid input");
}

Also, check that base and height are positive before calculating.

Can I write a Java program to find the area of a triangle given three points?

Yes, you can use the shoelace formula: Area = |(x1(y2 – y3) + x2(y3 – y1) + x3(y1 – y2))| / 2. This is useful in computational geometry. The result is the same as the base-height formula, but it works directly with coordinates.

Related

Formula used
in b | Base | cm | 10 | 0
in h | Height | cm | 5 | 0
out area | Area | cm² | (b * h) / 2 | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.