1.函数的递归
求某个学生中某个学生的年龄
#include <stdio.h>
//函数的递归
int getAge(int curPersonNum)
{
int age;
if(curPersonNum==1){
age = 10;
}else{
age = getAge(curPersonNum-1)+2;
}
return age;
}
int main()
{
int age;
int num;
printf("你想要知道第几个学生的年龄:\n");
scanf("%d",&num);
age = getAge(num);
printf("第%d学生的年龄是%d \n",num,age);
return 0;
}
2.求n的阶乘
#include <stdio.h>
//求n的阶乘
int getFactorial(int num){
int result;
//停止条件
if(num==1){
result = 1;
}else{
result = getFactorial(num-1)*num; //
}
return result;
}
int main()
{
int num;
int ret;
puts("输入你想求某个数的阶乘:");
scanf("%d",&num);
ret= getFactorial(num);
printf("%d的阶乘是:%d\n",num,ret);
return 0;
}