《C语言入门经典》Ivor Horton 第九章 练习题

//习题9.1 函数原型:
//double power(doubel x,int n);
//会计算并返回x^n。因此power(5.0,4)会计算5.0*5.0*5.0*5.0,
//它的结果是625.0、将power()函数实现为递归函数,再用适当的main()版本演示它的操作。
#include<stdio.h>
#include<stdbool.h>
double power(double x,int n);
int main(void)
{
  while (true)
  {
    int choose=0;
    printf("\nEnter 1 continue or 2 exit:");
    scanf("%d",&choose);
    fflush(stdin);
    if(choose==1)
    {
      double x=0.0;
      int n=0;
      printf("\nPplease enter the base x and n index");
      scanf("%lf %d",&x,&n);
      printf("\nThe result is %lf",power(x,n));
    }
    else if (choose==2)
    {
      exit(1);
    }
    else 
      printf("error");


  }
return 0;
}
double power(double x,int n)
{
  if(n<2)
    return x;
  return x*power(x,n-1);
}

你可能感兴趣的:(递归,函数,C语言)