Q:

Java program to calculate the area of Trapezium

belongs to collection: Java Basic Programs

0

Java program to calculate the area of Trapezium

All Answers

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

In this program, we will read base1base2height from the user and calculate the area of the Trapezium. Then we will print the result.

Program/Source Code:

The source code to calculate the area of a Trapezium is given below. The given program is compiled and executed successfully.

// Java program to calculate the 
// area of Trapezium

import java.util.Scanner;

public class Main {
  static float calcuateAreaOfTrapezium(float base1, float base2, float height) {
    float result = 0.0F;

    result = 0.5F * (base1 + base2) * height;

    return result;
  }

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

    float base1 = 0;
    float base2 = 0;
    float height = 0;
    float area = 0;

    System.out.printf("Enter base1: ");
    base1 = SC.nextInt();

    System.out.printf("Enter base2: ");
    base2 = SC.nextInt();

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

    area = calcuateAreaOfTrapezium(base1, base2, height);

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

Output:

Enter base1: 2
Enter base2: 4
Enter height: 5
Area of Trapezium is: 15.000000

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 calcuateAreaOfTrapezium() and main().

The calcuateAreaOfTrapezium() method is used to calculate the area of the Trapezium.

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 Trapezium using the calcuateAreaOfTrapezium() 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 Rhombus... >>
<< Java program to calculate the area of a triangle b...