Signup/Sign In
PUBLISHED ON: MAY 4, 2021

Java Program to find Area of a Triangle When Three Sides are Given

In this tutorial, we will see how to calculate the area of the triangle when the three sides are given. This can be done by using heron's formula. Heron’s formula is used to find the area of a triangle when we know the length of all its sides. It is also known as Hero’s Formula. But before moving forward, if you are not familiar with the concept of data types in java then do check the article on Datatypes in Java.

According to 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.

Let us look at the examples for a better understanding.

Program 1: Java Program to find Area of a Triangle when Three Sides are Given

In this program, we will see how to calculate the area of the triangle when the three sides are given using heron's formula.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the triangle sides.
  4. Ask the user to initialize these variables.
  5. Use Heron's Formula to Calculate the area of the triangle.
  6. First, calculate the semi-perimeter of the triangle.
  7. Then, calculate the area using the formula.
  8. Display the result.
  9. Stop

The below example illustrates the implementation of the above algorithm.

// Java Program to find Area of Triangle when three sides are given
import java.util.Scanner;

public class Main 
{
	public static void main(String[] args) 
	{
		double Perimeter, s, Area;
		Scanner sc = new Scanner(System.in);
		
		System.out.println("\n Enter the Three sides of triangle: ");
		double side1 = sc.nextDouble();
		double side2 = sc.nextDouble();
		double side3 = sc.nextDouble();

		Perimeter = side1 + side2 + side3;
		s = Perimeter/2; 
		Area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));


		System.out.format("\n The Perimeter of the Triangle = %.2f\n", Perimeter);
		System.out.format("\n The Semi Perimeter of Triangle = %.2f\n",s);
		System.out.format("\n The Area of the triangle = %.2f\n",Area);
	}
}


Enter the Three sides of the triangle: 5 6 7

The Perimeter of Triangle = 18.00

The Semi Perimeter of Triangle = 9.00

The Area of the triangle = 14.70

Program 2: Java Program to find Area of a Triangle when Three Sides are Given

In this program, we will see how to calculate the area of a triangle when three sides are given using a user-defined method.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the triangle sides.
  4. Ask the user to initialize these variables.
  5. Use a user-defined method to calculate the area of the triangle.
  6. Call the method and return the calculated area.
  7. Use heron's formula to calculate the area.
  8. Display the result.
  9. Stop.

The below example illustrates the implementation of the above algorithm.

// Java Program to find Area of Triangle when three sides are given
import java.util.Scanner;
public class Main 
{
    private static double area;
	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		
		System.out.println("\n Enter the three sides of the triangle: ");
		double side1 = sc.nextDouble();
		double side2 = sc.nextDouble();
		double side3 = sc.nextDouble();
		area = CalculateArea(side1, side2, side3);
		System.out.format("\n The Area of the triangle = %.2f\n",area);
	}
	public static double CalculateArea(double side1, double side2, double side3) 
	{
		double s;
		s = (side1 + side2 + side3)/2;
		area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
		return area;
	}
}


Enter the three sides of the triangle: 4 3 5

The Area of the triangle = 6.00



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.