用于打印每个底指数的 C 函数 例如,integerPower( 3, 4 ) = 3 * 3 * 3 * 3

标签 c

我的程序似乎无法运行

编写一个函数integerPower(base, exponent) 返回值 基指数。例如,integerPower( 3, 4 ) = 3 * 3 * 3 * 3。假设 指数是正数,非零 整数,基数是整数。函数integerPower应该用于 控制计算。 不要使用任何数学库函数。

我有这个程序

#include<stdio.h>

int power(int b, int e){

int x
for (x = 0; x <= e; x++)
    b=b;

return b;
}

void main(){
int b = 0;
int e = 0;

scanf("input a base %d\n", &b);
scanf("input an exponent %d\n", &e);

power(b,e);

printf("%d" , b);

}

最佳答案

在循环中

for (x = 0; x <= e; x++)
   b=b;   

b=b;没有用。它只是将b 的值分配给自己。您需要将 b 乘以 e 倍。为此,您需要采用另一个具有初始值 1 的变量,并在循环的每次迭代中将其乘以 b 以获得 be
将您的功能更改为此

int power(int b, int e){
    int x, y = 1;
    for (x = 1; x <= e; x++)
        y = y*b;  // Multiply e times

    return y;
}

关于用于打印每个底指数的 C 函数 例如,integerPower( 3, 4 ) = 3 * 3 * 3 * 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26712892/

相关文章:

c - 用于大小分析的 GCC 工具?

c - 暗网Yolo : Segmentation fault (core dumped) when calling draw_detections function

c++ - 如何使用 Linux 工具找到导致声明的包含链?

c - 如何修复C中的 'Program terminated with signal SIGBUS, Bus error'

c - 在 Doxygen 代码块中演示 Doxygen 的使用

c - 在c中生成url并发布数据

c - 写入管道总是失败

c++ - 调试 C++ 与调试 C 相比

大型矩阵的 CUDA 矩阵乘法中断

c - 我怎样才能使用c获取连接在同一网络中的机器的名称和IP地址?