C++pow
area = pow(4.0,2);
图 1 pow 函数的返回结果
area = pow(4.0,2)
Area = 423
x = 3 * pow(6.0, 3);
cout << pow(5.0, 4);
图 2 可以将 pow 函数理解为一个“黑箱”
程序必须包含 cmath 头文件;
在传递给函数的两个参数中,至少第一个参数应该是 double 类型的,当然也可以两个都是;
因为 pow 函数返回一个 double 值,所以被赋值的任何变量也应该是 double 类型的;
area = pow(4.0, 2);// This program calculates the area of a circle. The formula for the area of a circle is PI times the radius squared. PI is 3.14159. #include
#include // Needed for the pow function
using namespace std;
int main()
{
double area, radius;
cout << "This program calculates the area of a circle. \n";
// Get the radius
cout << "What is the radius of the circle? ";
cin >> radius;
// Compute and display the area
area = 3.14159 * pow(radius, 2);
cout << "The area is " << area << endl;
return 0;
}
This program calculates the area of a circle.
What is the radius of the circle? 10
The area is 314.159
area = 3.14159 * radius * radius;