ในบทความนี้ เราจะมาทำความเข้าใจว่าการหาพื้นที่ของวงกลมนั้นเป็นอย่างไร พื้นที่ของวงกลมคำนวณโดยใช้สูตร −
pie*radius*radius. i.e. πr2 Where π = 3.14 and r is the radius of the circle.
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
ป้อนข้อมูล
สมมติว่าข้อมูลที่เราป้อนคือ −
Radius of the circle : 5
ผลผลิต
ผลลัพธ์ที่ต้องการจะเป็น −
The Area of Circle is: 78.57142857142857
อัลกอริทึม
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
ตัวอย่างที่ 1
ที่นี่ ผู้ใช้ป้อนอินพุตตามข้อความแจ้ง คุณสามารถลองใช้ตัวอย่างนี้ในเครื่องมือกราวด์ของเรา
.
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);
}
} ผลลัพธ์
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
ตัวอย่างที่ 2
ในที่นี้ มีการกำหนดจำนวนเต็มก่อนหน้านี้ และเข้าถึงและแสดงค่าบนคอนโซล
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);
}
} ผลลัพธ์
The radius of the circle is 5.00000000 The Area of Circle is: 78.57142857142857