如何在C ++中使用PI常量?
在这里,我们将看到如何在C++程序中使用PI常量。PI常数存在于cmath头文件中。该常量的名称是M_PI。我们可以简单地包含该头文件,并使用常量执行操作。
在下面的示例中,我们将看到如何使用PI常数查找圆的面积。
范例程式码
#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);
}输出结果
Area of a circle with radius 7 unit is: 153.938