C++编程基础二 06-递归函数

 1 // C++函数和类 06-递归函数.cpp: 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include <string>
 7 #include <climits>
 8 #include <array>
 9 #include <math.h>
10 
11 using namespace std;
12 long fact(int i);
13 int main()
14 {
15     int num;
16     cout << "请输入一个10以内的正整数:" << endl;
17     cin >> num;
18     long res = fact(num);
19     cout << num << "的阶乘为:"<<res << endl;
20     return 0;
21 }
22 
23 long fact(int i)
24 {
25     long temp;
26     if (i == 0)
27     {
28         temp = 1;
29     }
30     else
31     {
32         temp = i * fact(i - 1); 
33     }
34     return temp;
35 }

猜你喜欢

转载自www.cnblogs.com/uimodel/p/9348580.html