Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

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:

Java Programs

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.