Java Program to Calculate Area of Triangle

Hello coders, I hope you are well and getting benefits from Tutorialsfield’s tutorials. In the previous tutorial, we have learned Java Program to calculate Area and Circumference of Circle. In today’s tutorial, we will learn Java Program to Calculate Area of Triangle.

Calculating the area of the triangle in Java is very easy and can be done in a few simple steps. Let’s see them.

Before writing the program, we need to know the formula for calculating the area of a triangle. The formula is here –

Area  of triangle = 1/2 * base * height

Now let’s start writing code. Here is the complete Java Program to Calculate Area of Triangle, and the explanations are given after the program.

Java Program to Calculate Area of Triangle

What we did ?

  • First of all we have imported the util package.
  • Then created a class and named it Triangle.
  • After that started main() method of the program.
  • Then inside main() method declared three variables width, height, and area which we will use in this program.
  • Then created object of the Scanner class.
  • Now asked the user to enter the width of the triangle. Then read the value of width entered by the user.
  • Now asked the user to enter the height of the triangle. Then read the value of height entered by the user.
  • Then calculated area of the triangle using the formula area = (width*height)/2.
  • Then finally printed the value of area of triangle on the screen.

Output:

So guys, we have learned Java Program to Calculate Area of Triangle. In the next tutorial we will learn Java Program to calculate Area of Triangle using heron’s formula.


Related Tutorials…

Leave a Comment