举一个用递归调用函数求输入非负整数的阶乘的例子,如下:
//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
int fact(int n){
if(n==1 || n==0) return 1;
else return n*fact(n-1);
}
int main(void){
int x;
while(1){
printf("Input x(int 12>=x>=0)...\nx=");
if(scanf("%d",&x),x>=0 && x<=12)//x>12时会使结果溢出
break;
printf("Error,redo: ");
}
printf("%d! = %d\n",x,fact(x));
return 0;
}