Java Program to Find Area of Triangle

In this tutorial, we will develop a Java program to find the area of a triangle. Generally, we find the area of a triangle having three sides, or the area of the right-angle triangle.

Program to find the area of the right-angle triangle in Java

Area of Right angle triangle or Triangle whose base and height is known, their area is given by,

Area=(1/2) * base * height

Procedure to develop a program to find the area of a right-angle triangle:-

1) Define class and the main method
2) Declare variables for taking inputs:- height and length
3) Import Scanner class of util package to read inputs
4) Read inputs from end-user and store them in the declared variables
5) Calculate the area of the right angle triangle using the formula, and store it in a variable
6) Display the result
7) Close the Scanner class object

import java.util.Scanner;

public class RightAngleTraingle {

   public static void main(String[] args) {

       // declare variables
       double height = 0.0;
       double base = 0.0;
       double area = 0.0;

       // create Scanner class object
       Scanner scan = new Scanner(System.in);

       // read input
       System.out.print("Enter height of the triangle:: ");
       height = scan.nextDouble();
       System.out.print("Enter base of the triangle:: ");
       base = scan.nextDouble();

       // calculate area
       area = (0.5) * (base * height);

       // display result
       System.out.println("Area = "+ area);

       // close Scanner class object
       scan.close();

    }
 }

Output:-

Enter height of the triangle:: 25.5
Enter base of the triangle:: 20
Area = 255.0

In this program, we had written logic to find the area of the right-angle triangle inside the main method, but we should not do that. It was given only for understanding purposes. We should write the logic inside a method and call them from the main method. In the below program we had written that logic separately in the user-defined method.

Java program to find the area of a triangle given three sides

Triangle having three sides their area is given by Heron’s Formula for the area of a triangle. If a, b and c are sides of triangles, and s is semi-perimeter then from Heron’s Formula,

s = (a+b+c) / 2

and,

area = √(s*(s-a)*(s-b)*(s-c))

To find the square root value we can take the support of sqrt() method of Math class. The sqrt() method of Math class is a static method so we can call it without creating the object of Math class.

import java.util.Scanner;

public class RightAngleTraingle {

   public static double findTriangleArea(double a, double b, double c){
       double s = 0.0;
       double area = 0.0;
       s = (a+b+c) / 2;
       area = Math.sqrt( s* (s-a) * (s-b) * (s-c) );
       return area;
   }

   public static void main(String[] args) {

       // declare variables
       double a = 0.0, b = 0.0, c = 0.0;
       double area = 0.0;

       // create Scanner class object
       Scanner scan = new Scanner(System.in);

       // read input
       System.out.print("Enter three sides of the triangle:: ");
       a = scan.nextDouble();
       b = scan.nextDouble();
       c = scan.nextDouble();

       // call method to calculate area
       area = findTriangleArea(a, b, c);       

       // display result
       System.out.println("Area = "+ area);

       // close Scanner class object
       scan.close();

   }
 }

Output for the different test-cases:-

Enter three sides of the triangle:: 4.5 8.9 12
Area = 16.643689494820542

Enter three sides of the triangle:: 3 4 5
Area = 6.0

Here we developed a java program to find the area of a triangle using methods. We define findTriangleArea(-,-,-) which takes three double type arguments and also returns double type value.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *