KnowledgeBoat Logo

Computer Applications

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}

Java

Java Conditional Stmts

72 Likes

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

Answered By

34 Likes


Related Questions