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);
}
}
Area of triangle is 25.0
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. double
variable area
. Finally, we print the area of the triangle using a formatted string.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);
}
}
Enter the width of the Triangle:
8
Enter the height of the Triangle:
10
Area of Triangle is: 40.0