Area of Triangle in Java
Calculate the area of a triangle using base and height.
BeginnerModule 1: Basic Java ProgramsExample 11 of 20
area-of-triangle.java
1import java.util.Scanner;23public class Main {4 public static void main(String[] args) {5 Scanner sc = new Scanner(System.in);67 System.out.print("Enter base: ");8 double base = sc.nextDouble();9 System.out.print("Enter height: ");10 double height = sc.nextDouble();1112 double area = 0.5 * base * height;13 System.out.println("Area of triangle = " + area);1415 sc.close();16 }17}
Output
Enter base: 10 Enter height: 5 Area of triangle = 25.0
What's going on
We use the formula area = 1/2 × base × height.