Trong bài này, chúng ta sẽ hiểu cách tìm diện tích của một hình tròn. Diện tích hình tròn được tính bằng công thức -
pie*radius*radius. i.e. πr2 Where π = 3.14 and r is the radius of the circle.
Dưới đây là một minh chứng về điều tương tự -
Đầu vào
Giả sử đầu vào của chúng tôi là -
Radius of the circle : 5
Đầu ra
Đầu ra mong muốn sẽ là -
The Area of Circle is: 78.57142857142857
Thuật toán
Step 1 – START Step 2 – Declare 2 double variables area and radius Step 3 – Read the value of the radius from the user Step 4- Calculate the area of the circle by using the formula (22*radius*radius)/7 and store the result as area Step 5- Display the area value 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 AreaOfCircle{
public static void main(String args[]){
double area, radius;
System.out.println("Required packages have been imported");
Scanner my_scanner= new Scanner(System.in);
System.out.println("A Scanner object has been defined ");
System.out.println("Enter the radius of the circle:");
radius= my_scanner.nextDouble();
area=(22*radius*radius)/7 ;
System.out.println("The Area of Circle is: " + area);
}
} Đầu ra
Required packages have been imported A Scanner object has been defined Enter the radius of the circle: 5 The Area of Circle is: 78.57142857142857
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 AreaOfCircle{
public static void main(String args[]){
double area, radius;
radius= 5;
System.out.printf("The radius of the circle is %.8f ", radius);
area=(22*radius*radius)/7 ;
System.out.println("\nThe Area of Circle is: " + area);
}
} Đầu ra
The radius of the circle is 5.00000000 The Area of Circle is: 78.57142857142857