Google News
logo
Java Program to Calculate area of Triangle
In the following examples of Java program to calculate the area of a triangle :
Program :
public class TriangleArea {

    public static void main(String[] args) {
        double base = 10;
        double height = 5;
        double area = 0.5 * base * height;
        System.out.println("Area of triangle is " + area);
    }
}
Output :
Area of triangle is 25.0
In this program, we first declare two double variables base and height and initialize them with the values 10 and 5 respectively. We then calculate the area of the triangle by multiplying the base and height, and then dividing the result by 2.

We store the final result in a double variable area. Finally, we print the area of the triangle using a formatted string.

Example 2 :
Program :
import java.util.Scanner;
class AreaTriangleDemo {
   public static void main(String args[]) {   
      Scanner scanner = new Scanner(System.in);

      System.out.println("Enter the width of the Triangle:");
      double base = scanner.nextDouble();

      System.out.println("Enter the height of the Triangle:");
      double height = scanner.nextDouble();

      //Area = (width*height)/2
      double area = (base* height)/2;
      System.out.println("Area of Triangle is: " + area);      
   }
}
Output :
Enter the width of the Triangle:
8
Enter the height of the Triangle:
10
Area of Triangle is: 40.0