1.编写计算X的Y次幂的递归函数getpower(int x,int ),并在主程序中实现输入输出。 #include"stdio.h"
long getpower(int x,int y)
{
if(y==1)
return x;
else
return x*getpower(x,y-1);
}
void main()
{
int num,power;
long answer;
printf("please input a number:");
scanf("%d",&num);
printf("please input the number's power series:");
scanf("%d",&power);
answer=getpower(num,power);
printf("结果是:%ld\n",answer);
}
结果说明:输入61再输入5求得61的5次幂为844596301.
2编写计算学生年龄的递归函数。
#include<stdio.h>
int age(int n)
{
int c;
if(n==1)
c=10;
else
c=age(n-1)+2;
return c;
}
void main()
{
int n=5;
printf("the five student'sage is:%d years old\n",age(n));
}