In this tutorial, you will learn how to write a Java program to find the area of a triangle. We will use two different formulas to find the area of a triangle:
- Standard Base-Height Formula: Used when the base length and perpendicular height are known.
- Heron’s Formula: Used when all three side lengths (a, b, and c) are known.
Before jumping into the program code, let’s review the mathematical formulas for calculating the area of a triangle.
Formula for Area of a Triangle
1. Base and Height Given
If the base and height of a triangle are given, the formula for calculating its area is:
Area = ½ × base × height
Here:
- base represents the base of the triangle.
- height represents the perpendicular height of the triangle.
- area represents the calculated area of the triangle.
For example, if the base of a triangle is 10 units and its height is 6 units:
Area = ½ × 10 × 6 = 30 square units
In Java, we can use the double data type when the base or height may contain decimal values.
2. Three Sides Given (Heron’s Formula)
If the lengths of all three sides of a triangle are given, we can use Heron’s formula to calculate its area.
Heron’s formula is:
Area = √[s × (s − a) × (s − b) × (s − c)]
where the semiperimeter s is calculated as:
s = (a + b + c) / 2
Here:
- a represents the length of the first side of the triangle.
- b represents the length of the second side of the triangle.
- c represents the length of the third side of the triangle.
- s represents the semiperimeter of the triangle.
- area represents the area of the triangle.
For example, suppose the three sides of a triangle are 5 units, 6 units, and 7 units.
First, calculate the semiperimeter:
s = (a + b + c) / 2
= (5 + 6 + 7) / 2
= 18 / 2
= 9 unitsNow, apply Heron’s formula:
Area = √[s × (s − a) × (s − b) × (s − c)]
= √[9 × (9 − 5) × (9 − 6) × (9 − 7)]>
= √[9 × 4 × 3 × 2]
= √216
≈ 14.70 square unitsTherefore, the area of the triangle is approximately 14.70 square units. In Java, we can use the Math.sqrt() method to calculate the square root when implementing Heron’s formula.
Java Program to Find Area of Triangle
Problem Statement: Write a Java program to calculate the area of a triangle and display the result on the console. In this basic approach, we directly assign values to the base and height inside the program, then print the result to the console.
Complete Java Program
// Java program to find the area of a triangle.
public class TriangleArea {
public static void main(String[] args) {
// Declare and initialize the base and height
double base = 10.0;
double height = 6.0;
// Calculate the area of the triangle
double area = (base * height) / 2;
// Display the base, height, and calculated area
System.out.println("--- Triangle Details ---");
System.out.println("Base: " + base);
System.out.println("Height: " + height);
System.out.println("Area of Triangle: " + area);
}
}Output:
--- Triangle Details --- Base: 10.0 Height: 6.0 Area of Triangle: 30.0
Explanation of the Program
Step 1: Declare and initialize the base
double base = 10.0;
Here, we declare a variable named base using the double data type and initialize it with the value 10.0. We use double so that the program can also work with decimal values such as 10.5 or 12.75.
Step 2: Declare and initialize the height
double height = 6.0;
Here, we declare a variable named height and initialize it with the value 6.0. The height represents the perpendicular height of the triangle.
Step 3: Calculate the area of the triangle
double area = (base * height) / 2;
Here, we calculate the area using the formula:
Area = (base × height) / 2
The values of base and height are multiplied first, and the result is then divided by 2. The calculated value is stored in the area variable.
Step 4: Display the result
System.out.println("Area of Triangle: " + area);The System.out.println() method displays the calculated area on the console.
Java Program to Find Area of Triangle Using Heron’s Formula
Problem Statement: When the lengths of all three sides of a triangle are given, but its perpendicular height is not known, we can use Heron’s formula to calculate the area of the triangle.
Let’s write a Java program to calculate the area of a triangle when the lengths of its three sides are given.
Complete Java Program
// Java program to find the area of a triangle using Heron's formula.
public class TriangleHeron {
public static void main(String[] args) {
// Declare and initialize the three sides
double a = 5.0;
double b = 6.0;
double c = 7.0;
// Calculate the semiperimeter
double s = (a + b + c) / 2.0;
// Calculate the area using Heron's formula
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
// Display the triangle details
System.out.println("--- Triangle Details ---");
System.out.println("Side a: " + a);
System.out.println("Side b: " + b);
System.out.println("Side c: " + c);
System.out.println("Semiperimeter: " + s);
System.out.println("Area of Triangle: " + area);
}
}Output:
--- Triangle Details --- Side a: 5.0 Side b: 6.0 Side c: 7.0 Semiperimeter: 9.0 Area of Triangle: 14.696938456699069
Explanation of the Program
Step 1: Declare the three sides
double a = 5.0; double b = 6.0; double c = 7.0;
Here, we declare three variables named a, b, and c to store the lengths of the three sides of the triangle. We use the double data type because the side lengths may contain decimal values.
Step 2: Calculate the semiperimeter
double s = (a + b + c) / 2;
The semiperimeter of the triangle is calculated using the following formula:
s = (a + b + c) / 2
For the given values:
s = (5 + 6 + 7) / 2 = 9
Therefore, the semiperimeter is 9.0.
Step 3: Calculate the area
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
Here, we implement Heron’s formula in Java.
The Math.sqrt() method is used to calculate the square root of the value inside the square root symbol.
For the given values:
Area = √[9 × (9 − 5) × (9 − 6) × (9 − 7)] = √216 ≈ 14.70
Step 4: Display the result
System.out.println("Area of Triangle: " + area);This statement displays the calculated area of the triangle on the console.





