Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để tìm diện tích của một hình thang

Trong bài này, chúng ta sẽ hiểu cách tìm diện tích hình thang. Hình thang là loại tứ giác có ít nhất một cặp cạnh đối song song với nhau. Các cạnh song song của hình thang được gọi là đáy và các cạnh không song song của hình thang được gọi là chân. Nó còn được gọi là hình thang.

Diện tích hình thang được tính theo công thức -

(height/2 * (side_1 + side_2).
i.e.
Area = ½ x (sum of the lengths of the parallel sides) x perpendicular distance between parallel
sides

Dưới đây là một minh chứng về điều tương tự. Diện tích hình thang với độ dài các cạnh song song a, b và chiều cao h của hình thang được cho bởi -

Chương trình Java để tìm diện tích của một hình thang

Đầu vào

Giả sử đầu vào của chúng tôi là -

side_1 = 5
side_2 = 6
height = 6

Đầu ra

Đầu ra mong muốn sẽ là -

Area of trapezium is: 33.0

Thuật toán

Step 1 - START
Step 2 – Declare three integer values namely side_1 , side_2 and height. Declare a float value
namely my_area.
Step 3 - Read the required values from the user/ define the values
Step 4 – Calculate the area of the trapezium using the formula (height/2 * (side_1 + side_2)
and store the result
Step 5- Display the result
Step 6- Stop

Ví dụ 1

Ở đây, đầu vào đang được người dùng nhập dựa trên lời nhắc. Bạn có thể thử trực tiếp ví dụ này trong công cụ nền tảng mã hóa của chúng tôi Chương trình Java để tìm diện tích của một hình thang .

import java.util.Scanner;
public class AreaOfTrapezium {
   public static void main(String args[]){
      int side_1 , side_2 , height ;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the length of the first parallel side: ");
      side_1 = my_scanner.nextInt();
      System.out.print("Enter the length of the first parallel side : ");
      side_2 = my_scanner.nextInt();
      System.out.print("Enter the heigth of the trapezium : ");
      height = my_scanner.nextInt();
      float my_area = (height/2 * (side_1 + side_2));
      System.out.println("The area of trapezium is: " + my_area);
   }
}

Đầu ra

Required packages have been imported
A reader object has been defined
Enter the length of the first parallel side: 5
Enter the length of the first parallel side : 6
Enter the heigth of the trapezium : 6
The area of trapezium is: 33.0

Ví dụ 2

Ở đây, số nguyên đã được xác định trước đó và giá trị của nó được truy cập và hiển thị trên bảng điều khiển.

public class AreaOfTrapezium {
   public static void main(String args[]){
      int side_1 = 5, side_2 = 6, height = 6;
      System.out.println("The sides and height of the trapezium is defined as " +side_1 + ", " + side_2 + " and " + height);
      float my_area = (height/2 * (side_1 + side_2));
      System.out.println("The area of Trapezium is: " + my_area);
   }
}

Đầu ra

The sides and height of the trapezium is defined as 5, 6 and 6
The area of Trapezium is: 33.0