Q:

Java program to calculate the area of a triangle based on given three sides

belongs to collection: Java Basic Programs

0

Java program to calculate the area of a triangle based on given three sides

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will read three sides of the triangle from the user and find the area of the triangle. Then we will print the result.

Program/Source Code:

The source code to calculate the area of a triangle based on the given three sides is given below. The given program is compiled and executed successfully.

// Java program to calculate the area of a triangle 
// based on given three sides

import java.util.Scanner;

public class Main {
  static double calcuateAreaTriangle(int a, int b, int c) {
    double s = 0;
    double area = 0;

    s = (double)(a + b + c) / 2;
    area = Math.sqrt(s * (s - a) * (s - b) * (s - c));

    return area;
  }

  public static void main(String[] args) {
    Scanner SC = new Scanner(System.in);

    int a = 0;
    int b = 0;
    int c = 0;

    double area = 0;

    System.out.printf("Enter the First edge of Triangle: ");
    a = SC.nextInt();

    System.out.printf("Enter the Second edge of Triangle: ");
    b = SC.nextInt();

    System.out.printf("Enter the Third edge of Triangle: ");
    c = SC.nextInt();

    area = calcuateAreaTriangle(a, b, c);

    System.out.printf("Area of a triangle: %f\n", area);
  }
}

Output:

Enter the First edge of Triangle: 12
Enter the Second edge of Triangle: 10
Enter the Third edge of Triangle: 8
Area of a triangle: 39.686270

Explanation:

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contain two static methods calculateAreaTriangle() and main().

The calculateAreaTriangle() method is used to calculate the area of a triangle based on the given edges of the triangle.

The main() method is an entry point for the program. Here, we read the edges of the triangle from the user using the Scanner class. Then we calculated the area of the triangle using the calculateAreaTriangle() method and printed the result.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to calculate the area of a triangle b... >>
<< Java program to find the (LCM) Lowest Common Multi...