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 bình hành

Trong bài này, chúng ta sẽ hiểu cách tìm diện tích của hình bình hành Một hình bình hành có hai cặp cạnh đối diện song song bằng nhau. Nó có đáy và chiều cao là khoảng cách vuông góc giữa mặt đáy và cạnh song song đối diện của nó.

Diện tích hình bình hành được tính bằng công thức -

base * height
i.e.
b x h

Dưới đây là một minh chứng về điều tương tự -

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

Đầu vào

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

Base : 6
Height : 8

Đầu ra

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

Area parallelogram is : 48

Thuật toán

Step 1 - START
Step 2 - Declare three integer values values namely
Step 3 - Read the required values from the user/ define the values
Step 4 - Calculate the area using the formula by base * height 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 bình hành .

import java.util.Scanner;
public class AreaOfParallelogram{
   public static void main(String args[]){
      int base, height, my_area;
      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 value of parallelogram base : ");
      base = my_scanner.nextInt();
      System.out.print("Enter the value of parallelogram base : ");
      height = my_scanner.nextInt();
      my_area=base*height;
      System.out.println("The area of the parallelogram is : "+my_area);
   }
}

Đầu ra

Required packages have been imported
A reader object has been defined
Enter the value of parallelogram base : 6
Enter the value of parallelogram base : 8
The area of the parallelogram is : 48

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 AreaOfParallelogram{
   public static void main(String args[]){
      int base, height;
      base=6;
      height=8;
      System.out.println("The base and height values are defined as " +base +" and " +height);
      int my_area=base*height;
      System.out.println("The area of the parallelogram is : "+my_area);
   }
}

Đầu ra

The base and height values are defined as 6 and 8
The area of the parallelogram is : 48