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 -- 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.
- 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)
- 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);
}
}
}
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
- The base and the height are
doublevalues, 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 as0.5, not1 / 2, because1 / 2is 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
printfwith%.2fwrites 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);
}
}
}
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
- The three sides are read as
doublevalues with aScanner. - The
booleanisTriangleistrueonly when every side is positive and each side is shorter than the sum of the other two. If it isfalse, the program prints a message and calculates nothing. - For a real triangle,
sis half the perimeter, and the area is the square root ofs * (s - a) * (s - b) * (s - c). Math.sqrt()Click to read: Math.sqrt() gives the square root, andprintfprints 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);
}
}
Enter the side: 5 Area of the equilateral triangle: 10.83 Enter the side: 6 Area of the equilateral triangle: 15.59
- The side is read as a
doublewith aScanner; 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, andprintfprints 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.General Java Programs on JavaBrahman
Fahrenheit to CelsiusClick to Read Fahrenheit to Celsius Tutorial FactorialClick to Read Factorial Tutorial FibonacciClick to Read Fibonacci Tutorial PalindromeClick to Read Palindrome Tutorial Armstrong NumberClick to Read Armstrong Number Tutorial Automorphic NumberClick to Read Automorphic Number Tutorial Harshad NumberClick to Read Harshad Number Tutorial Matrix AdditionClick to Read Matrix Addition Tutorial Matrix MultiplicationClick to Read Matrix Multiplication Tutorial
Fahrenheit to CelsiusClick to Read Fahrenheit to Celsius Tutorial FactorialClick to Read Factorial Tutorial FibonacciClick to Read Fibonacci Tutorial PalindromeClick to Read Palindrome Tutorial Armstrong NumberClick to Read Armstrong Number Tutorial Automorphic NumberClick to Read Automorphic Number Tutorial Harshad NumberClick to Read Harshad Number Tutorial Matrix AdditionClick to Read Matrix Addition Tutorial Matrix MultiplicationClick to Read Matrix Multiplication Tutorial