/*
Enter 2 values for X and Y separated by space, The press <enter>: 2.4 4.85
2.4 to the power of 4.85 is = 69.8272
Enter 2 values for X and Y separated by space, The press <enter>: 3 4
3 to the power of 4 is = 81
*/
#include <iostream>
#include <cmath>
double power(double x, int count);
int main() {
using std::cout;
using std:: cin;
double x , y;
double p;
cout << "Enter 2 values for X and Y separated by space, The press <enter>: ";
cin >> x >> y;
int tmp = y;
if (y == tmp)
p = power(x, tmp);
else
p = pow(x, y);
cout << x << " to the power of " << y << " is = " << p << "\n";
return 0;
}
double power(double x, int count) {
double p = 1;
while (count) {
p *= x;
count--;
}
return p;
}
求一个数的N次方
最新推荐文章于 2023-11-22 18:17:02 发布
本文介绍了一个使用C++实现的简单程序,该程序能够计算任意基数和指数的幂,并展示了如何处理整数和浮点数指数的情况。通过自定义幂运算函数与标准库pow函数的结合使用,实现了灵活的指数计算。
537

被折叠的 条评论
为什么被折叠?



