我写了一些代码来计算 100 的结果!但是我的函数在 22 之后停止给出正确答案!这是我的代码。任务是计算 100 位的总和!但我的代码给出者在 22 之后的错误答案!
/*Calculates the factorial of a given number*/
double factorial(double num);
/*Calculates how many digits are in a number*/
int num_digits(double num);
int main (int argc, char *argv[]) {
double ans = factorial(23);
int num = num_digits(ans);
printf("%lf\n", ans);
printf("%d\n", num);
char x[num];
//convert the number returned by factorial() to a char array
sprintf (x, "%lf", ans);
int i = 0;
int sum= 0;
for (i = 0; i < num; i ++) {
sum += x[i] - '0';
}
printf("--\n%d\n", sum);
return 0;
}
double factorial(double num) {
int i;
double sum = 1;
/*heel simpel, gwn 1 x sum, sum * sum - 1 enz..*/
for (i = num; i > 0; i --) {
sum *= i;
}
return sum;
}