Java Program to Find Area of Triangle

In the previous article we have discussed Java Program to Find Volume of Prism

In this article we will discuss how to find the area of a triangle in different ways.

Program to Find Area of Triangle

Before jumping into the program directly, first let’s see how to find the area of a triangle.

Area formula of triangle: (width*height)/2

Example:

Example-1

When the width of the triangle = 20.0 
and the height of the triangle = 110.5
Then area of the triangle  => (width*height)/2 
                                          => (20.0*110.0)/2 
                                          = 1105.0
Example-2

When the width of the triangle = 3
and the height of the triangle = 4
Then area of the triangle => (width*height)/2 
                                         => (3*4)/2
                                         = 6

Let’s see different ways to find find area of triangle:

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Method-1 : Java Program to Find Area of Triangle Using Static Value

In this the values of width and height are static values, means the values are already defined in the program. So, let’s see the program to understand it more clearly.

import java.util.*;

class Main
{
   public static void main(String args[])
   {   // width of triangle
      double width = 20.0;
      // hight of triangle
      double height = 100.5;
      //finding area
      double area = (width * height)/2;
      // printing the area
      System.out.println("Area of Triangle: " + area);      
    }
}
Output:
Area of Triangle: 1005.0

Method-2 : Java Program to Find Area of Triangle Using User Defined Value

In this the values of width and height are dynamic values, means it will ask the user for the input values in the program. So, let’s see the program to understand it more clearly.

import java.util.*;

class Main
{
   public static void main(String args[]) 
    {   
       
       Scanner sc= new Scanner(System.in);
        
        System.out.println("Enter the width of triangle:");
        double width= sc.nextDouble();
 
        System.out.println("Enter the height of triangle:");
        double hight= sc.nextDouble();
 
       //Area = (width*height)/2
       double area=(width*hight)/2;
       System.out.println("Area of triangle: " + area);      
   }
}
Output:

Enter the width of triangle: 2
Enter the height of triangle: 4
Area of triangle: 4.0

Method-3: Java Program to Find Area of Triangle Using Constructor

In this the width and height values will be taken by the user and those values will be passed as parameter to the constructor where the actual area finding logic is present.

So, let’s see the program to understand it more clearly.

import java.util.*;


class TriangleArea
{
      long area;
      // Constructor
      TriangleArea(long width,long hight)
    {
      //finding area
      area=(width*hight)/2;  
    }
}
 
//Driver Class
class Main
{
   public static void main(String args[]) 
    {   
      Scanner sc= new Scanner(System.in);
      // Asking user to take width input
      System.out.println("Enter the width of triangle:");
      long width= sc.nextLong();
      // Asking user to take hight input
      System.out.println("Enter the height of triangle:");
      long hight= sc.nextLong();
      TriangleArea t=new TriangleArea(width,hight);
      System.out.println("Area of triangle: " + t.area);      
   }
}
Output:

Enter the width of triangle: 4
Enter the height of triangle: 4
Area of triangle: 8

Method-4: Java Program to Find Area of Triangle Using User Defined Method

In this the width and height values will be taken by the user and those values will be passed as parameter to a user defined method where the actual area finding logic is present.

So, let’s see the program to understand it more clearly.

import java.util.*;
 
//Driver Class
class Main
{
   public static void main(String args[]) 
    {   
      Scanner sc= new Scanner(System.in);
      // Asking user to take width input
      System.out.println("Enter the width of triangle:");
      long width= sc.nextLong();
      System.out.println("Enter the height of triangle:");
      long hight= sc.nextLong();
      //calling the method
      TriangleArea(width,hight);
    }
     
     //user defined method  
    public static void TriangleArea(long width,long hight)
    {
      long area=(width*hight)/2;  
      System.out.println("Area of triangle: " + area); 
    }
    
}
Output:

Enter the width of triangle: 4
Enter the height of triangle: 4
Area of Triangle: 8

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Programs: