Java Program to find Area of a Right Angled Triangle

Write a Java Program to find Area of a Right Angled Triangle with example.

Java Area of a Right Angled Triangle

  • If we know the width and height then, we can calculate the area of a right angled triangle using the below formula. Area = (1/2) * width * height
  • Using Pythagoras formula, we can easily find the unknown sides in the right angled triangle. c² = a² + b²
  • The perimeter of a Right Angled Triangle is the distance around the edges. We can calculate the perimeter using the below formula. Perimeter = a + b+ c

Java Program to find Area of a Right Angled Triangle Example

This Java program allows the user to enter the width and height of the right angled triangle. Using those values, we will calculate the Area and perimeter of the right angled triangle.

// Java Program to find Area of a Right Angled Triangle Example 

package Area;

import java.util.Scanner;

public class AreaOfRightTriangle {
	private static Scanner sc;

	public static void main(String[] args) {
		double width, height, c, Area, Perimeter; 
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter the Width of a Right Angled Triangle: ");
		width = sc.nextDouble();
		System.out.println("\n Please Enter the Height of a Right Angled Triangle: ");
		height = sc.nextDouble();

		Area = 0.5 * width * height;
		c = Math.sqrt((width * width) + (height * height));
		Perimeter = width + height + c;

		System.out.format("\n The Area of a Right Angled Triangle = %.2f\n",Area);
		System.out.format("\n The Other side of a Right Angled Triangle is: %.2f\n",c);
		System.out.format("\n The Perimeter of a Right Angled Triangle = %.2f\n", Perimeter);
	}
}
Java Program to find Area of a Right Angled Triangle 1

The following Java statements allow the user to enter the Width and Height of a right angled triangle and assign those values to corresponding variables.

System.out.println("\n Please Enter the Width of a Right Angled Triangle: ");
width = sc.nextDouble();
System.out.println("\n Please Enter the Height of a Right Angled Triangle: ");
height = sc.nextDouble();

Next, we are calculating the area of a right angled triangle. The value of 1/2 = 0.5) So, we used 0.5 * width * height as the formula

Area = 0.5 * width * height;

In the next line, We are calculating the other side of a right angled triangle using the Pythagoras formula C² = a² + b², which is similar to C = √a²+b². Here we used Java Math.sqrt() function to calculate the square root of the a²+b²

c = Math.sqrt((width * width) + (height * height));

In the next line, We are calculating the perimeter using the formula

Perimeter = width + height + c;

The following System.out.format statements help us to print the Perimeter, Another side, and Area of a right angled Triangle.

System.out.format("\n The Area of a Right Angled Triangle = %.2f\n",Area);
System.out.format("\n The Other side of a Right Angled Triangle is: %.2f\n",c);
System.out.format("\n The Perimeter of a Right Angled Triangle = %.2f\n", Perimeter);

Java Program to find Area of a Right angled triangle using Functions

This Java program uses the logic that we specified in the first example. But we will separate the logic and place it in a method.

package Area;

import java.util.Scanner;

public class AreaOfRightTriangleUsingMethods {
	private static Scanner sc;

	public static void main(String[] args) {
		double width, height; 
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter the Width of a Right Angled Triangle: ");
		width = sc.nextDouble();
		System.out.println("\n Please Enter the Height of a Right Angled Triangle: ");
		height = sc.nextDouble();
		
		AreaofRightTriangle(width, height);

	}
	public static void AreaofRightTriangle( double width, double height ) {
		double c, Area, Perimeter; 
		
		Area = 0.5 * width * height;
		c = Math.sqrt((width * width) + (height * height));
		Perimeter = width + height + c;

		System.out.format("\n The Area of a Right Angled Triangle = %.2f\n",Area);
		System.out.format("\n The Other side of a Right Angled Triangle is: %.2f\n",c);
		System.out.format("\n The Perimeter of a Right Angled Triangle = %.2f\n", Perimeter);
	}
}

Java Area of a Right Angled Triangle output

 Please Enter the Width of a Right Angled Triangle: 
10

 Please Enter the Height of a Right Angled Triangle: 
12

 The Area of a Right Angled Triangle = 60.00

 The Other side of a Right Angled Triangle is: 15.62

 The Perimeter of a Right Angled Triangle = 37.62

Java Program to find Area of a Right angled triangle using Oops

In this Java area of a right angled triangle example, we are dividing the code using the Object Oriented Programming. To do this, First, we will create a class that holds methods.

package Area;

public class AreaOfaRightTriangle {
	double c, Area, Perimeter; 
	public void AreaofRightTriangle( double width, double height ) {
		Area = 0.5 * width * height;
		c = Math.sqrt((width * width) + (height * height));
		Perimeter = width + height + c;

		System.out.format("\n The Area of a Right Angled Triangle = %.2f\n",Area);
		System.out.format("\n The Other side of a Right Angled Triangle is: %.2f\n",c);
		System.out.format("\n The Perimeter of a Right Angled Triangle = %.2f\n", Perimeter);
	}
	
	public double RightTriangle( double width, double height ) {
		Area = 0.5 * width * height;
		return Area;
	}
}

Within the Main Java Program to calculate Area of a Right Angled Triangle, we will create an instance of the above-specified class and call the methods.

package Area;

import java.util.Scanner;

public class AreaOfRightTriangleUsingClass {
	private static Scanner sc;

	public static void main(String[] args) {
		double Area, width, height; 
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter the Width of a Right Angled Triangle: ");
		width = sc.nextDouble();
		System.out.println("\n Please Enter the Height of a Right Angled Triangle: ");
		height = sc.nextDouble();
		
		AreaOfaRightTriangle art = new AreaOfaRightTriangle();
		art.AreaofRightTriangle(width, height);
		Area = art.RightTriangle(width, height);
		
		System.out.format("\n Second Method: The Area of a Right Angled Triangle = %.2f\n", Area);
	}

}
 Please Enter the Width of a Right Angled Triangle: 
7

 Please Enter the Height of a Right Angled Triangle: 
8

 The Area of a Right Angled Triangle = 28.00
 The Other side of a Right Angled Triangle is: 10.63
 The Perimeter of a Right Angled Triangle = 25.63
 Second Method: The Area of a Right Angled Triangle = 28.00

Comments are closed.