×

Java Programs

Java Practice

Java program to calculate the area of Trapezium

Given base1, base2, and height, we have to calculate the area of Trapezium.
Submitted by Nidhi, on February 27, 2022

Problem statement

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

Java program to calculate the area of Trapezium

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.

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.