- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
Conditional Constructs in Java
Chapter 8
Conditional Constructs in Java
Class 10 - Logix Kips ICSE Computer Applications with BlueJ
![]() Share with a Friend |
Java Program: Compute the perimeter and area of a triangle using Heron’s formula
27. Write a program in Java to compute the perimeter and area of a triangle, with its three sides given as a, b, and c using the following formulas:
import java.util.Scanner;
public class TrianglePerimeterArea {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a, b, c;
double perimeter, s, area;
System.out.print("Enter side a: ");
a = sc.nextDouble();
System.out.print("Enter side b: ");
b = sc.nextDouble();
System.out.print("Enter side c: ");
c = sc.nextDouble();
// Calculate perimeter
perimeter = a + b + c;
// Calculate semi-perimeter
s = perimeter / 2;
// Calculate area using Heron's formula
area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
System.out.println("\nPerimeter of the triangle = " + perimeter);
System.out.println("Area of the triangle = " + area);
}
}
Output
Sample Output Enter side a: 3 Enter side b: 4 Enter side c: 5 Perimeter of the triangle = 12.0 Area of the triangle = 6.0
✅ Explanation
- The program takes three sides as input using the Scanner class.
- Perimeter is calculated by adding all sides.
- Semi-perimeter (s) is half of the perimeter.
- Area is calculated using Math.sqrt() and Heron’s formula.
- Suitable for school exams, assignments, and practicals.
