Write a Java Program to find the Area of a Triangle with an example. If we know the length of three sides of a triangle, we can calculate the area of a triangle using Heron’s Formula:
Area of a Triangle = √(s*(s-a)*(s-b)*(s-c))
Where s = (a + b + c)/2 (Here s = semi perimeter and a, b, c are the three sides of a triangle)
Perimeter of a Triangle = a + b + c
Java Program to find Area of Triangle and Perimeter of Triangle
This Java program allows the user to enter three sides of the triangle. Using those values, we will calculate the Perimeter of a triangle, Semi Perimeter of a triangle, and then the Area of a Triangle.
import java.util.Scanner; public class AreaOfTriangle { private static Scanner sc; public static void main(String[] args) { double a, b, c, Perimeter, s, Area; sc = new Scanner(System.in); System.out.println("\n Please Enter Three sides of triangle: "); a = sc.nextDouble(); b = sc.nextDouble(); c = sc.nextDouble(); Perimeter = a + b + c; s = (a + b + c)/2; // Perimeter/2 Area = Math.sqrt(s*(s-a)*(s-b)*(s-c)); System.out.format("\n The Perimeter of Traiangle = %.2f\n", Perimeter); System.out.format("\n The Semi Perimeter of Traiangle = %.2f\n",s); System.out.format("\n The Area of triangle = %.2f\n",Area); } }
Within the following statements, System.out.println statement will ask the user to enter the three sides of the triangle a, b, c. Next, we assign those users values to a, b and c
System.out.println("\n Please Enter Three sides : "); a = sc.nextDouble(); b = sc.nextDouble(); c = sc.nextDouble();
In the next Java line, we used a mathematical formula to calculate the Perimeter of the Triangle using the formula P = a + b + c.
Perimeter = a + b + c;
Next, we are calculating the semi perimeter using the formula (a + b + c) / 2. Although we can write semi perimeter = (Perimeter/2), we want to show the formula behind it. That’s why we used the standard formula.
s = (a + b + c)/2;
Next, we calculate the area of a triangle using Heron’s Formula. Here Math.sqrt() is the math function, which is to calculate the square root.
Area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
NOTE: Please be careful while placing the open and close brackets, it may change the entire calculation if you place it wrong
Java Program to find Area of Triangle using functions
This Java program allows the user to enter three sides. We will pass those three values to the function arguments to calculate the area of a triangle.
import java.util.Scanner; public class AreaOfTriangleUsingMethods { private static Scanner sc; private static double Ar; public static void main(String[] args) { double a, b, c; sc = new Scanner(System.in); System.out.println("\n Please Enter Three sides : "); a = sc.nextDouble(); b = sc.nextDouble(); c = sc.nextDouble(); Ar = ArofaTri(a, b, c); System.out.format("\n The Area of triangle = %.2f\n",Area); } public static double ArofaTri( double a, double b, double c ) { double s; s = (a + b + c)/2; Ar = Math.sqrt(s*(s-a)*(s-b)*(s-c)); return Ar; } }
In this example, We declared the static function of double type with three arguments. By using those arguments, this function will calculate the area of the Triangle.
Please Enter Three sides :
5
6
7
The Area of triangle = 14.70
Java Program to find Area of Triangle using Oops
This program finds the area using the triangle’s three sides. We are dividing the above example code using Object Oriented Programming in this example. To do this, First, we will create a class that holds methods.
public class AreaofaTriangle { double a, b, c, Perimeter, s, Area; public void TriangleArea() { Perimeter = a + b + c; s = (a + b + c)/2; Area = Math.sqrt(s*(s-a)*(s-b)*(s-c)); System.out.format("The Perimeter of Triangle = %.2f\n", Perimeter); System.out.format("The Semi Perimeter of Triangle = %.2f\n",s); System.out.format("The Area of triangle = %.2f\n",Area); } public double AreaofTriangle( double a, double b, double c ) { double s; s = (a + b + c)/2; Area = Math.sqrt(s*(s-a)*(s-b)*(s-c)); return Area; } }
Within the Main program, we will create an instance of the above-specified class and call the methods.
import java.util.Scanner; public class AreaOfTriangleUsingClass { private static Scanner sc; public static void main(String[] args) { double x, y, z, Area; sc = new Scanner(System.in); System.out.println("Please Enter Three sides of triangle: "); x = sc.nextDouble(); y = sc.nextDouble(); z = sc.nextDouble(); AreaofaTriangle at = new AreaofaTriangle(); at.a = x; at.b = y; at.c = z; at.TriangleArea(); Area = at.AreaofTriangle(x, y, z); System.out.format("Second Method: The Area of triangle = %.2f\n", Area); } }
Please Enter Three sides of triangle:
7
8
9
The Perimeter of Triangle = 24.00
The Semi Perimeter of Triangle = 12.00
The Area of triangle = 26.83
Second Method: The Area of triangle = 26.83