Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

วิธีใช้ค่าคงที่ PI ใน C++

บทนำ

ค่าคงที่ PI อยู่ในไฟล์ header cmath ชื่อค่าคงที่คือ M_PI เราเพียง include header นั้นและใช้ค่าคงที่ได้เลย

ตัวอย่างโค้ด

#include <iostream>
#include <cmath>
using namespace std;

float area(int radius) {
    return M_PI * (radius * radius);
}

int main() {
    cout << "Area of a circle with radius 7 unit is: " << area(7);
    return 0;
}

ผลลัพธ์

Area of a circle with radius 7 unit is: 153.938