ICSE Class 10 Computer Applications Question 27 of 43

Conditional Constructs in Java — Question 27

Back to all questions
27
Question

Question 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:

Perimeter=a+b+c\text{Perimeter} = a + b + c

Area=s(sa)(sb)(sc)\text{Area} = \sqrt{s(s - a)(s - b)(s - c)}

Where s=a+b+c2\text{Where} \space s = \dfrac{a+b+c}{2}

Answer
import java.util.Scanner;

public class KboatTriangle
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter first side: ");
        double a = in.nextDouble();
        System.out.print("Enter second side: ");
        double b = in.nextDouble();
        System.out.print("Enter third side: ");
        double c = in.nextDouble();
        
        double perimeter = a + b + c;
        double s = perimeter / 2;
        double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
        
        System.out.println("Perimeter = " + perimeter);
        System.out.println("Area = " + area);
    }
}
Output
BlueJ output of KboatTriangle.java