Site icon TestingDocs.com

Java program for area of triangle

Program Description

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

Area of Triangle

Mathematical formula:

 

Eclipse IDE Steps

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

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

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

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. 

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 :

https://www.oracle.com/in/java/

 

Exit mobile version