C++使用递归函数计算阶乘

时间:2014-12-18 21:59:01   收藏:0   阅读:233
 1 // 使用递归函数计算阶乘
 2 
 3 #include<iostream>
 4 using namespace std;
 5 int Factorial(int n);
 6 
 7 int main()
 8 {
 9     cout<<"计算n的阶乘:"<<endl;
10     int n;
11     cout<<"请输入n:"<<endl;
12     cin>>n;
13     cout<<"n的阶乘n!=  "<<Factorial(n)<<endl;
14     return 0;
15 }
16 int Factorial(int n)
17 {
18     if(n<=1)
19     {
20         return 1;
21     }
22     else
23     {
24         return Factorial(n-1)*n;
25     }
26 }

bubuko.com,布布扣

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!