C++:用两种不同的循环语句实现x的n次方求解

来源:百度知道 编辑:UC知道 时间:2024/06/14 07:19:31
急啊 明天早上交!!

一个用for循环,一个用while循环
运行过,一切正常~

#include <iostream>
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);

}

#include<stdio.h>
main()
{int x,n;
imt i=0;
int result;
scanf("%d %d",x,n);
while(i<n)
{result*=x;
i++;
}
printf(