pow()函数以及C ++中的示例
C ++pow()
函数
pow()函数是cmath标头(在早期版本中为<math.h> )的库函数,用于查找幂的加数,它接受两个参数并将第一个参数返回为第二个参数的幂。
pow()
函数语法:
pow(x, y);
参数: x,y –是计算x ^ y的数字。
返回值: double-返回double值,它是x的计算结果乘以y的幂。
示例
Input:float x = 2;
float y = 3;
Function call:
pow(x,y);
Output:
8
C ++代码演示pow()
函数示例
//示例// pow()功能
#include <iostream>
#include <cmath>
using namespace std;
//主代码部分
int main(){
float x;
float y;
//输入值
cout<<"Enter the value of x: ";
cin>>x;
cout<<"Enter the value of y: ";
cin>>y;
//计算幂
float result = pow(x,y);
cout<<x<<" to the power of "<<y<<" is = "<<result;
cout<<endl;
return 0;
}
输出结果
First run:Enter the value of x: 10
Enter the value of y: 5
10 to the power of 5 is = 100000
Second run:
Enter the value of x: 10.23
Enter the value of y: 1.5
10.23 to the power of 1.5 is = 32.72
Third run:
Enter the value of x: 10
Enter the value of y: 0
10 to the power of 0 is = 1
Fourth run:
Enter the value of x: 0
Enter the value of y: 1.5
0 to the power of 1.5 is = 0
以上是 pow()函数以及C ++中的示例 的全部内容, 来源链接: utcz.com/z/334670.html