编程实现幂函数,(指数为整数)
作者:互联网
结果如图:
代码如下:
int pow(int x, int y)
{
int i, t;
if (y == 0)
t = 1;
else
{
if (y < 0)
y = -y;
else
y = y;
t = 1;
for (i = 0; i < y; i++)
t = t * x;
}
return t;
}
#include<stdio.h>
int main()
{
int x, y, t;
printf("输入底数和指数:");
scanf("%d %d", &x, &y);
if (y < 0)
printf("1/%d\n", t = pow(x, y));
else
printf("%d\n", t = pow(x, y));
return 0;
}
标签:幂函数,return,int,pow,编程,整数,else,printf 来源: https://blog.csdn.net/qq_52510978/article/details/121890470