c语言递归求n的阶乘

1.用循环

#include
int main()
{
	int i,n,sum=1;
	scanf("%d",&n);
	for(i=1;i<=n;i++) {
		sum*=i; 
	}
	printf("%d!=%d",n,sum);
	
	getch();
	return 0;
}


2.用递归

#include

int factorial(int n){
	if(n==0)
		return 1;
	return factorial(n-1)*n;
}
int main()
{
	int n,sum=1;
	scanf("%d",&n);
	printf("%d!=%d",n,factorial(n));
	
	getch();
	return 0;
}



你可能感兴趣的:(c)