For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
46. Packages in Java
53. Java Collection
56. Generics In Java
57. Java Interfaces
60. Streams in Java
63. Thread in Java
67. Deadlock in Java
74. Applet in Java
75. Java Swing
76. Java Frameworks
78. JUnit Testing
81. Jar file in Java
82. Java Clean Code
86. Java 8 features
87. String in Java
93. HashMap in Java
98. Enum in Java
101. Hashcode in Java
105. Linked List in Java
109. Array Length in Java
111. Split in java
112. Map In Java
115. HashSet in Java
118. DateFormat in Java
121. Java List Size
122. Java APIs
128. Identifiers in Java
130. Set in Java
132. Try Catch in Java
133. Bubble Sort in Java
135. Queue in Java
142. Jagged Array in Java
144. Java String Format
145. Replace in Java
146. charAt() in Java
147. CompareTo in Java
151. parseInt in Java
153. Abstraction in Java
154. String Input in Java
156. instanceof in Java
157. Math Floor in Java
158. Selection Sort Java
159. int to char in Java
164. Deque in Java
172. Trim in Java
173. RxJava
174. Recursion in Java
175. HashSet Java
177. Square Root in Java
190. Javafx
The area of a triangle in Java can be calculated using basic arithmetic operations and built-in Java features. Java is an object-oriented programming language developers widely use for developing software applications.
Java offers platform independence, vast libraries, and frameworks, making it a popular choice among developers, especially for building robust, scalable, and portable applications for web, enterprise, mobile, and embedded systems.
You can easily compute the area of a triangle in Java. This can be done using formulas such as (base × height) / 2. For example, you can write a Java program that accepts user input for the base and height values and computes the triangle's area with the help of these values.
In this tutorial, we’ll discuss several methods of finding the area of a triangle in Java. Keep reading to know more.
If we know the base (b) and the height (h) of the triangle, we can use the formula:
Area = (b * h) / 2
If we know the lengths of all three sides (a, b, c), we can use Heron's formula:
Area = sqrt(s * (s - a) * (s - b) * (s - c))
where s = (a + b + c) / 2
If we know the coordinates of three vertices (x1, y1), (x2, y2), (x3, y3), we can use the shoelace formula:
Area = 0.5 * |(x1y2 + x2y3 + x3y1) - (x2y1 + x3y2 + x1y3)|
Here is an example of calculating the area of a triangle using the height and base of the triangle (base x height) in Java:
import java.util.Scanner;
public class upGradTutorials {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the base of the triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the triangle:");
double height = scanner.nextDouble();
double area = (base * height) / 2;
System.out.println("The area of the triangle is: " + area);
}
}
The program begins by importing the Scanner class from java.util package, allowing us to read user input. Next, a class named TriangleAreaCalculator is declared, which contains the main method, serving as the program's entry point.
Inside the main method, an instance of the Scanner class is created and associated with the standard input stream (System.in), enabling input reading. The program then prompts the user to enter the base of the triangle by printing the corresponding message. The entered value is read as a double using the nextDouble() method of the Scanner class and assigned to the base variable.
Similarly, the program prompts the user to enter the height of the triangle, reads the input value as a double, and assigns it to the height variable. The area of the triangle is calculated using the formula (base * height) / 2 and assigned to the area variable.
Finally, the program displays the calculated area of the triangle by printing the concatenation of the string "The area of the triangle is: " with the value of the area variable.
Here is an example of calculating the area of a triangle using the lengths of its three sides (Heron's formula) in Java:
import java.util.Scanner;
public class upGradTutorials {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of side a:");
double a = scanner.nextDouble();
System.out.println("Enter the length of side b:");
double b = scanner.nextDouble();
System.out.println("Enter the length of side c:");
double c = scanner.nextDouble();
double s = (a + b + c) / 2;
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
System.out.println("The area of the triangle is: " + area);
}
}
In the above example, the program prompts the user to enter the lengths of the triangle's three sides. It reads these values as double using the nextDouble() method of the Scanner class and assigns them to the variables a, b, and c.
Next, it calculates the semi-perimeter of the triangle (s) using the formula (a + b + c) / 2. Then, it applies Heron's formula to calculate the triangle area using the Math.sqrt() function to compute the square root.
Finally, the program displays the calculated area of the triangle by printing the concatenation of the string "The area of the triangle is: " with the value of the area variable.
Here is an example of calculating the area of a triangle in Java using a constructor:
import java.util.Scanner;
public class Triangle {
private double base;
private double height;
private double area;
public Triangle (double base, double height) {
this.base = base;
this.height = height;
}
public void calculateArea() {
area = (base * height) / 2;
}
public double getArea() {
return area;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the base of the triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the triangle:");
double height = scanner.nextDouble();
Triangle triangle = new Triangle(base, height);
triangle.calculateArea();
System.out.println("The area of the triangle is: " + triangle.getArea());
}
}
This example defines a Triangle class with private fields for base, height, and area. The class has a constructor that takes the base and height as parameters and assigns them to the corresponding fields.
We also have two methods within the Triangle class: calculateArea() and getArea(). The calculateArea() method calculates the area of the triangle using the formula (base * height) / 2 and stores the result in the area field. The getArea() method returns the value of the area field.
In the main method, we prompt the user to enter the base and height of the triangle and read those values using the Scanner class. Then, we create an instance of the Triangle class, passing the base and height as arguments to the constructor.
Next, we call the calculateArea() method on the triangle object to calculate the area. Finally, we display the calculated area of the triangle using the getArea() method. When the program runs, it asks the user to input the base and height of the triangle, creates a Triangle object, calculates the area using the provided values, and displays the resulting area of the triangle.
import java.util.Scanner;
public class upGradTutorials {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the base of the triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the triangle:");
double height = scanner.nextDouble();
double area = calculateTriangleArea(base, height);
System.out.println("The area of the triangle is: " + area);
}
public static double calculateTriangleArea(double base, double height) {
return (base * height) / 2;
}
}
In this program, we define a class called TriangleAreaCalculator. The main method prompts the user to enter the base and height of the triangle, reads those values using the Scanner class, and assigns them to the variables base and height.
The program then calls the calculateTriangleArea method, passing the base and height as arguments. The calculateTriangleArea method calculates the triangle area using the formula (base * height) / 2 and returns the result.
Finally, the calculated area is stored in the area variable and displayed to the user using the System.out.println statement.
Here is an example of calculating the area of a triangle in Java using object-oriented programming:
import java.util.Scanner;
public class Triangle {
private double sideA;
private double sideB;
private double sideC;
public Triangle(double a, double b, double c) {
sideA = a;
sideB = b;
sideC = c;
}
public double calculateArea() {
double s = (sideA + sideB + sideC) / 2;
return Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of side A:");
double sideA = scanner.nextDouble();
System.out.println("Enter the length of side B:");
double sideB = scanner.nextDouble();
System.out.println("Enter the length of side C:");
double sideC = scanner.nextDouble();
Triangle triangle = new Triangle(sideA, sideB, sideC);
double area = triangle.calculateArea();
System.out.println("The area of the triangle is: " + area);
}
}
In the above program, we define a Triangle class representing a triangle object. The Triangle class has private fields for sideA, sideB, and sideC, representing the lengths of the triangle's three sides.
The Triangle class has a constructor that takes sideA, sideB, and sideC as parameters and initializes the corresponding fields.
Additionally, the Triangle class has a calculateArea() method that calculates the area of the triangle using Heron's formula: Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC)), where s is the semi-perimeter of the triangle.
In the main method, we prompt the user to enter the lengths of the triangle's sides (sideA, sideB, and sideC) and read those values using the Scanner class. We then create a Triangle object by invoking the constructor with the provided side lengths.
Next, we call the calculateArea() method on the triangle object to calculate the area and store the result in the area variable. Finally, we display the calculated area of the triangle using the System.out.println statement.
Here is an example of calculating the area of a triangle in Java by implementing a Shape interface with an area() method:
import java.util.Scanner;
// Define an interface with an area() method
interface Shape {
void area();
}
// Triangle class implements the Shape interface
public class Triangle implements Shape {
private double base;
private double height;
// Constructor to initialize base and height
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
// Overridden method to calculate and print the area
public void area() {
double result = (base * height) / 2;
System.out.println("The area of the triangle is: " + result);
}
// Main method to execute the program
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the base of the triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the triangle:");
double height = scanner.nextDouble();
// Create a Triangle object and call area()
Triangle triangle = new Triangle(base, height);
triangle.area();
}
}
In this program, we define a Shape interface with an abstract method area(). The Triangle class implements the Shape interface and provides its own definition for the area() method.
The class also includes a constructor that initializes the base and height of the triangle. Inside the overridden area() method, we calculate the area using the formula (base * height) / 2 and print the result.
In the main method, we use the Scanner class to take input from the user for the base and height of the triangle. After reading the values, we create an instance of the Triangle class and call its area() method to calculate and display the area.
This example demonstrates the concept of abstraction in Java using interfaces while keeping the area calculation logic clean and modular.
The numerous ways of calculating the area of a triangle in Java have been discussed in this tutorial. You must enroll in a certified course if you want to use Java for more advanced functions. You could look for software development courses by upGrad. The courses on upGrad are well-designed for professionals and ensure you develop a holistic understanding of the subject. Enquire today to know more.
To find the area of a triangle in Java, use the formula (0.5 * base * height)
. Store base and height as floating-point values, then calculate and print the result using simple arithmetic and System.out.println()
.
The standard area of triangle formula in Java is:
area = 0.5 * base * height;
This formula is used in most beginner-level programs for calculating the area when base and height are provided.
To write a Java program to calculate the area of a triangle, take base and height as input using Scanner
, apply the formula, and display the result. This program helps understand variables, user input, and arithmetic operations.
Yes, you can calculate the area of triangle in Java using Scanner by taking user input for base and height, then applying the formula. Scanner allows real-time input from the console, making the program interactive.
Use double
for storing base, height, and area in your area of triangle Java program. This ensures precision, especially when working with decimal values or larger dimensions.
You can define a method like double calculateArea(double base, double height)
to return the area of triangle. This improves modularity and makes the code reusable for multiple triangle inputs.
A simple Java program to find area of triangle includes input handling using Scanner
, calculation using the area formula, and displaying the result using System.out.println()
. It’s ideal for beginners learning input/output and formulas.
Use Scanner
to read values for base and height. The user types the input in the console, and the program calculates the area of a triangle in Java based on those values.
Yes, Heron’s formula can be used in Java when you know all three sides. First, calculate the semi-perimeter, then apply Math.sqrt(s*(s−a)*(s−b)*(s−c))
to find the area. It’s useful for more advanced triangle problems.
Yes, the area of triangle program in Java is a common beginner-level exercise. It teaches user input, data types, basic math, and console output—all essential building blocks in Java development.
To handle invalid input in your Java program for area of triangle, use input validation techniques like checking for negative numbers or catching exceptions when input is not a number. This improves user experience and code robustness.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author|900 articles published
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.