Here we are explaining how to calculate the area of a triangle.
A triangle has three sides. The area of a triangle is calculated as a=(b*h)/2. Where b is the base length and h is the height of the triangle.
First, we have to declare the class AreaTriangle create an object of the scanner class as sc, and read the base length and height of the triangle from the user into the variable b,h using the nextDouble() method.Calulate the area as a=(b*h)/2.Display area as a by using system.out.println()
.
STEP 1: Declare the class AreaTriangle with a public modifier.
STEP 2: Open the main() to start the program, Java program execution starts with the main()
STEP 3: Declare the variables b,h, a as double.
STEP 4: Read the base width of the triangle as b.
STEP 5: Read the height of the triangle as h.
STEP 6: Calculate the area of the triangle as a= (b*h)/2.
STEP 7: Display area a.
import java.util.Scanner;
public class AreaTriangle{
public static void main(String args[]) {
double b,h,a;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the base width of the Triangle:");
b = sc.nextDouble();
System.out.println("Enter the height of the Triangle:");
h = sc.nextDouble();
a = (b* h)/2;
System.out.println("The Area of the Triangle is: " + a);
}
}
Enter the base width of the Triangle:10 Enter the height of the Triangle:15 The Area of the Triangle is: 75.0