java

Write a Java Program to Calculate the Area of Triangle

In this tutorial, we are going to see how to write a Java program to calculate the area of a triangle. We will use the following formula to calculate the area of the triangle: Area = (base * height) / 2
As you can see in the following image ‘b’ is the base and ‘h’ is the height of the triangle:
 


 
 

Java Program to Calculate the Area of Triangle:
import java.util.Scanner;

class Main {
   public static void main(String args[]) {   
      Scanner sc = new Scanner(System.in);

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

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

      double area = (base * height)/2;
      System.out.println("The area of the triangle is: " + area);      
   }
}

Output:

Enter the base of the triangle: 8
Enter the height of the triangle: 2
The area of the triangle is: 8.0
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *