首页 > 解决方案 > C语言递归阶乘不起作用

问题描述

对于下面显示的代码,我没有得到任何响应;我在没有条件的情况下保持最小化,例如想象人们会选择正数。除了:

进程返回 -1073741571 (0xC00000FD) 执行时间:3.194 s

如果我输入 5,答案应该是 120,而不是这里。

#include <stdio.h>
#include <stdlib.h>

int faktorijel(int x) {
    return (x*faktorijel(x-1));
}

main() {
    int a,b;
    printf("Type in a number:");
    scanf("%d\n", &a);
    b=faktorijel(a);
    printf("Result is %d\n", b);
    return 0;
}

标签: cfunctionrecursion

解决方案


你应该设置停止if

int faktorijel(int x){
        if (x == 1) {
          return 1;
        } else {
          return (x*faktorijel(x-1));
        }
}

推荐阅读