Trong bài này, chúng ta sẽ hiểu cách tìm diện tích hình vuông. Diện tích hình vuông được tính theo công thức sau -
side*side i.e. s2
Dưới đây là một minh chứng về điều tương tự -
Nếu cạnh của hình vuông là s, thì diện tích của hình vuông được cho bởi s 2 -
Đầu vào
Giả sử đầu vào của chúng tôi là -
Length of the side : 4
Đầu ra
Đầu ra mong muốn sẽ là -
Area of the square : 16
Thuật toán
Step 1 - START Step 2 - Declare 2 integer values namely my_side and my_area. Step 3 - Read the required values from the user/ define the values. Step 4 – Calculate the area of the square using the formula side*side 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
.
import java.util.Scanner;
public class AreaOfSquare {
public static void main(String args[]){
int my_side, 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 length of a side : ");
my_side = my_scanner.nextInt();
my_area = my_side* my_side;
System.out.println("The my_area of the square is :"+my_area);
}
} Đầu ra
Required packages have been imported A reader object has been defined Enter the length of a side : 4 The area of the square is :16
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 AreaOfSquare {
public static void main(String args[]){
int my_side, my_area;
my_side = 4;
System.out.println("The length of the side of the square is defined as : " +my_side);
my_area = my_side* my_side;
System.out.println("The my_area of the square is :"+my_area);
}
} Đầu ra
The length of the side of the square is defined as : 4 The area of the square is :16