C++怎样用for循环计算x的n次方?

#include <iostream>using namespace std;Int main()....
2025-03-19 12:53:51
推荐回答(1个)
回答1:

一个用for循环,一个用while循环
运行过,一切正常~
#include
using namespace std;
void __for(int x, int n)
{
double result = 1.0;
for(int i = 0; i < n; i++)
result *= x;
cout << x << "的" << n << "次方是:" << result << endl;
}
void __while(int x, int n)
{
double result = 1.0;
int j = 0;
while(n != 0)
{
result *= x;
n--;
j++;
}
cout << x << "的" << j << "次方是:" << result << endl;
}
void main()
{
int x;
int n;
cout << "输入x、n:";
cin >> x >> n;
__for(x, n);
__while(x, n);
}