首页

2010年5月29日星期六

C语言递归和迭代法

#include
int factorial(int n)
{
if(n == 0)
    {
      return 1;
    }
else{
    int recurse= factorial(n-1);
    int result = n* recurse;
    return result;
}
}
int main()
{
int number;
printf("Please input number:\n");
scanf("%d",&number);
printf("The is your input number factorial:%d\n",factorial(number));
}


#include
int factorial(int n)
{
       int result = 1;
       while (n > 0) {
              result = result * n;
              n = n - 1;
       }
       return result;
}
int main()
{
int num;
printf("Please input number:\n");
scanf("%d",&num);
printf("%d",factorial(num));
}



#include

int factorial(int n)
{
       int result = 1;
       int i = 1;
       do {
              result = result * i;
              i = i + 1;
       } while (i <= n);
       return result;
}
int main()
{
int number;
printf("Please input number:\n");
scanf("%d",&number);
printf("The is your input number factorial:%d\n",factorial(number));
}



#include

int factorial(int n)
{
int result=1;
int i;
for(i=1;i<=n;++i)
    {
      result = result*i;
    }
return result;
}
int main()
{
int number;
printf("Please input number:\n");
scanf("%d",&number);
printf("The is your input number factorial:%d\n",factorial(number));
}

没有评论:

发表评论