【C语言】求一个数的阶乘(递归)

【C语言】求一个数的阶乘(递归)

因为定义的int型数据,阶乘不要超过12 否则会溢出。

手机用户:

标题

 代码:

  1 #include "stdio.h"
  2 
  3 int Factorial(int n) {
  4 
  5     if(n == 1) {//使递归结束的条件
  6 
  7         return 1;//1! 等于1
  8 
  9     }else{//使递归进行的条件
 10 
 11         return(Factorial(n-1) * n);//n! = (n-1)! * n;
 12 
 13     }
 14 
 15 
 16 }
 17 
 18 
 19 int main() {
 20 
 21     int i;
 22     printf("Enter Number!:");
 23     scanf("%d",&i);
 24     printf("Factorial = %d\n",Factorial(i));
 25     return 0;
 26 }

结果:

猜你喜欢

转载自blog.csdn.net/u011182346/article/details/84225709
今日推荐