All Posts

Java Program for Area of Triangle

Java Program for Area of Triangle

Write a Java program to calculate the area of a triangle. The area of the triangle of base b, and height h is calculated using the mathematical formula:

Area of Triangle

Mathematical formula:

Area of Triangle = \frac{ base * height}{ 2}

Eclipse IDE Steps

We will use Eclipse IDE in this tutorial. Some steps and instructions to create a Java application:

  • Create Java Project in Eclipse

https://www.testingdocs.com/create-a-new-java-project-in-eclipse/

  • Create Java Package in Eclipse

https://www.testingdocs.com/create-java-package-in-eclipse-ide/

  • Create Java Class in Eclipse

https://www.testingdocs.com/create-a-new-java-class-in-a-project/

 

Java Program

The Java program prompts the user to enter the base and the height of the triangle. It computes the area of the triangle using the formula. The program displays the area to the console window.

 

import java.util.Scanner;
/*****************************************************************
 * AreaOfTriangle.java
 * @program : Java program to calculate the area
 * @web : www.testingdocs.com
 * @version : 1.0
 ******************************************************************/ 
public class AreaOfTriangle 
{
  public static void main(String args[]) 
  {   

    Scanner s= new Scanner(System.in);

    System.out.println("Enter the width of the Triangle:");
    double b= s.nextDouble();

    System.out.println("Enter the height of the Triangle:");
    double h= s.nextDouble();

    //Area = (width*height)/2
    double area=(b*h)/2;
    System.out.println("Area of Triangle is: " + area);      
  }
}

 

 

Run the Program

Run the Java application to verify the program.

  • Steps to Run Java Project in Eclipse IDE

https://www.testingdocs.com/run-java-project-in-eclipse/

Sample Output

Enter the width of the Triangle:
45
Enter the height of the Triangle:
65
Area of Triangle is: 1462.5

Screenshot

Screenshot of the Java Application:

 

—

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :