TestingDocs.com
Testing Questions
  • Home
  • Automation
    • Selenium
    • TestNG
  • Tutorials
    • MySQL Tutorials
    • TestLink
    • Maven
    • Git
  • Flowcharts
    • Flowgorithm
    • Raptor

Java Programs

Java program for area of triangle

Eclipse

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:

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 :

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

 

Related Posts

Add Two Numbers Flow Chart

Java Programs /

Write a simple java program to add two numbers?

Fahrenheit To Celsius

Java Programs /

Java program to Convert Fahrenheit to Celsius

Factorial Java Program using Recursion

Java Programs /

Factorial Java Program using Recursion

Area of Rectangle

Java Programs /

Write a Java program to calculate Area of Rectangle

Volume of a Cylinder

Java Programs /

Volume of a Cylinder Java Program

‹ Factorial Java Program using Recursion› Java program to Convert Fahrenheit to Celsius

Recent Posts

  • What are the advantages of PHP?
  • What are the advantages of JavaScript?
  • How to Hide Taskbar on Windows 11
  • How to edit PATH variable on Windows 11?
  • Enable BitLocker Drive Encryption on Windows 11
  • How to lock Windows 11 PC
  • How to Add, Remove Widgets on Windows 11
  • How to Rename PC on Windows 11?
  • How to Shut down Windows 11 PC?
  • How to launch command prompt on Windows 11

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com