Java program to calculate the area of triangle


How to calculate area of a triangle in Java

This is another basic java example that calculates the area of triangle.
The formula for the triangle area is:








The program inputs the base and height of the triangle and finds the area.

This is the java program to find the area of a triangle:
//Area of triangle Java
import java.util.Scanner;
public class TriangleArea {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        double base, height, area;

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

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

        area = base * height / 2;

        System.out.println("Area = " + area);
    }
}
Sample output:
Enter the base of the triangle: 2,4
Enter the height of the triangle: 3,6
Area = 4.32


No comments :

Post a Comment