如何在 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP