方法一: 使用 whil...
方法一: 使用 while
#include <stdio.h>
int main()
{
int base, exponent;
long long result = 1;
printf("基数: ");
scanf("%d", &base);
printf("指数: ");
scanf("%d", &exponent);
while (exponent != 0)
{
result *= base;
--exponent;
}
printf("结果:%lld", result);
return 0;
}
方法二:使用 pow() 函数
#include <stdio.h>
#include <math.h>
int main()
{
double base, exponent, result;
printf("基数: ");
scanf("%lf", &base);
printf("指数: ");
scanf("%lf", &exponent);
// 计算结果
result = pow(base, exponent);
printf("%.1lf^%.1lf = %.2lf", base, exponent, result);
return 0;
}
方法三:递归
#include <stdio.h>
int power(int n1, int n2);
int main()
{
int base, powerRaised, result;
printf("基数: ");
scanf("%d",&base);
printf("指数(正整数): ");
scanf("%d",&powerRaised);
result = power(base, powerRaised);
printf("%d^%d = %d", base, powerRaised, result);
return 0;
}
int power(int base, int powerRaised)
{
if (powerRaised != 0)
return (base*power(base, powerRaised-1));
else
return 1;
}
参考方法:
#include<stdio.h>
int main()
{
int i,a,b;
unsigned long c=1;
printf("请输入基数:");
scanf("%d",&a);
printf("请输入指数");
scanf("%d",&b);
for(i=1;i<=b;i++)
{
c *= a;
}
printf("%lu",c);
return 0;
}
登录后提交答案