Hello coders, in this post, we will learn Java program to calculate area and perimeter of Right Triangle. In the previous tutorial, you have learned Java program to calculate area of a triangle using Heron’s formula. We can calculate the area and perimeter of the right triangle in Java very easily in just a few lines of code.
So let’s try to know the answer to the query, how do you find the area and perimeter of a right triangle in Java?
But first, let’s know the formula of finding the area and perimeter of a right triangle. Here is the formula.
Area of right triangle = (b*h)/2
Where,
- b is the base of the triangle
- h is the base and height of the triangle.
Perimeter of right triangle = a+b+c
where
- a, b, and c are three sides of the triangle.
Now Let us write the program.
Java Program to calculate Area and Perimeter of Right Triangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.*; public class Triangle { public static void main(String[] args) { double a, b, c, base, height, area, perimeter; Scanner in = new Scanner(System.in); // Asking the user to enter the base and height of the triangle System.out.println(" Enter the base and height of the triangle :"); base = in.nextDouble(); height = in.nextDouble(); System.out.println(" Enter three sides of the triangle :"); a = in.nextDouble(); b = in.nextDouble(); c = in.nextDouble(); area = (base*height)/2; // Calculating area of triangle perimeter = a+b+c; // Calculating perimeter of triangle System.out.println(" Area of Right Triangle : " + area +" square units"); System.out.println(" Perimeter of Right Triangle : " + perimeter); } } |
What we did?
- First of all we have imported util package.
- Then created a class and named it Triangle.
- After that started main() method of the program.
- Then inside main() method declared variables.
- Then created object of the Scanner class.
- Now asked the user to enter the base and height of the triangle. Then read the value of base and height entered by the user.
- Then calculated area of triangle using the formula area = (base*height)/2.
- In the next line calculated perimeter of triangle using the formula perimeter = a+b+c
- Then finally printed the value of area and perimeter of triangle on the screen.
Output
1 2 3 4 5 6 |
Enter the base and height of the triangle : 2 4 Enter three sides of the triangle : 6 2 1 Area of Right Triangle : 4.0 square units Perimeter of Right Triangle : 9.0 |
So guys this was all about Java program to calculate area and perimeter of Right Triangle. In the next tutorial you will learn Java Program to calculate Area and Perimeter of Rectangle.
Related Tutorials…
- Java Program to Calculate Area and Circumference of Circle
- Java Program to calculate Area of Triangle
- Java Program to calculate Area of Triangle using heron’s formula
- Java Program to calculate Area and Perimeter of Square
- Java Prorgram to calculate Area and Perimeter of Parallelogram
- Java Program to calculate Area of Trapezium
- Java Program to calculate Area and Volume of Cube
- Java Program to calculate Area and Volume of Cuboid
- Java Program to calculate Total Surface Area and Curved Surface Area of cylinder
- Java Program to calculate Volume of Cone
- Java Program to calculate Surface Area and Volume of Sphere