我是 C 编程语言的新手,我正在尝试学习递归来计算给定数字的阶乘。我的问题是如果我输入“5” ,调试printf
语句正在打印。2,6,24,120
如果函数调用被替换为相应的值并一次计算阶乘,它如何打印 4 次?
#include<stdio.h>
#include<stdlib.h>
int factorial(int n);
int main()
{
int num;
int fact_val;
printf("Enter the number for which you are going to compute the factorial:");
scanf("%d",&num);
fact_val=factorial(num);
printf("The factorial of the given number is %d\n",fact_val);
return 0;
}
int factorial(int n)
{
int factorial_val;
if(n==1)
return 1;
else
{
factorial_val=factorial(n-1)*n;
printf("Debugger-%d\n",factorial_val);
}
return factorial_val;
}