c - 我的阶乘函数和幂函数在不应该输出数百万的情况下输出了数百万,我不知道如何修复它(在 c 中)

标签 c function factorial

I am trying to write a sort of calculator program and i can get everything but my factorial and power functions to work. They output a number in the millions no matter how small the number is and i don't see a problem with the code. (I just started learning C recently so assume the extent of my knowledge is everything in this code)

int iFactorial(num1){//needs help returns a number in the millions no matter what

    int i, factorial=1;
    printf("Enter a positive number: ");
    scanf("%d", &num1);
    for(i=1; i<=num1; i++)
            factorial*=i;
            printf("The factorial is %i", &factorial);
    return 0;

}
int fPower(num1,num2){//needs help, same as above
    int i, number = 1;
    printf("Enter the number you want to raise to a power: \n");
    scanf("%d", &num1);
    printf("Enter the exponent: ");
    scanf("%d", &num2);

    for(i=0; i<num2; i++)
        number*=num1;
        printf("%d to the %d equals %d", &num1, &num2, &number);
    return 0;  
}

最佳答案

您在 print 语句中使用 & 来打印所用变量的地址。 更正各自函数中的语句如下:

printf("The factorial is %i", factorial);

printf("%d to the %d equals %d", num1, num2, number);

关于c - 我的阶乘函数和幂函数在不应该输出数百万的情况下输出了数百万,我不知道如何修复它(在 c 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48669023/

相关文章:

c - 使用 CMake 生成 SWIG 绑定(bind)

javascript - 单击功能,窗口高度在加载和调整大小时可变

java - 打印我的递归阶乘结果

java - 系列代

recursion - 简单的 LISP 代码不会捕获输入错误

c++ - 来自第二个线程的 Qt 信号调用有效 -> 对连接的插槽没有影响

c - 为结构中的字符串分配内存

c - 将数组划分为 K 个子数组,差异最小

javascript - FullCalendar.io。无法呈现背景事件

c - 编写采用非空 double 组及其长度作为参数并返回 : 的函数