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/