หน้าหลัก » ภาษาซีพลัสพลัส (C++) » C++ คำนวณหาพื้นที่วงกลม

C++ คำนวณหาพื้นที่วงกลม




ตัวอย่างโค้ดภาษา C++ ในการคำนวณหาพื้นที่ของวงกลม

ตัวอย่างโค้ดภาษา C++

#include <iostream>
#include <cmath>

int main() {
    double radius, area;

    std::cout << "Enter the radius of the circle: ";
    std::cin >> radius;

    area = M_PI * std::pow(radius, 2);

    std::cout << "The area of the circle is: " << area << std::endl;

    return 0;
}

อธิบายเพิ่มเติม

  • M_PI คือตัวแทนของค่าพาย (π)
  • std::pow(radius, 2) ใช้เพื่อหาค่ารัศมียกกำลังสอง

แสดงผล


Enter the radius of the circle: 10
The area of the circle is: 314.159


Enter the radius of the circle: 2.5
The area of the circle is: 19.635