1.递归求阶乘
代码:
#include<iostream>
using namespace std;
int main()
{
int jiecheng(int a);
int n;
cin >> n;
if (n == 0)
{
cout << 1 << endl;
}
else
cout << jiecheng(n) << endl;
return 0;
}
int jiecheng(int a)
{
if (a == 1)
{
return 1;
}
else
{
while (a > 0)
{
return a*jiecheng(a - 1);
}
}
}
结果:
代码二:
#include"stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int jiecheng(int a);
int n;
cout << "Please input a number: " << endl;
cin