Q:

Java program to calculate the area of a triangle based on a given base and height

belongs to collection: Java Basic Programs

0

Java program to calculate the area of a triangle based on a given base and height

All Answers

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

In this program, we will read the base, height 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 base and height is given below. The given program is compiled and executed successfully.

// Java program to calculate the area of a 
// triangle based on given base and height

import java.util.Scanner;

public class Main {
  static int calcuateAreaTriangle(int base, int height) {
    return (base * height) / 2;
  }

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

    int base = 0;
    int height = 0;
    int area = 0;

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

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

    area = calcuateAreaTriangle(base, height);
    System.out.printf("Area of a triangle: %d\n", area);
  }
}

Output:

Enter the base of Triangle: 12
Enter the height of Triangle: 7
Area of a triangle: 42

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 contains two static methods calculateAreaTriangle() and main().

The calculateAreaTriangle() method is used to calculate the area of the triangle based on the given base and height.

The main() method is an entry point for the program. Here, we read base, height from the user using 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 Trapezium... >>
<< Java program to calculate the area of a triangle b...