因子程序c使用带有while循环的C中的递归函数

时间:2012-02-11 19:23:37

标签: c recursion factorial

使用while循环在c中使用递归的因子程序。在该程序中,一旦执行到达函数return语句,它将不会返回函数调用。相反,它重复执行该功能。任何人都可以告诉我这个程序有什么问题。

#include<stdio.h>    
int fact(int n)
{    
    int x=1;    

    while(n>1)    
    {    
        x=n*fact(n-1);    
    }    

    return(x);    
}    

void main()    
{    
    int n,fact1;    
    scanf("%d",&n);    
    fact1=fact(n);    
    printf("%d",fact1);    
}

10 个答案:

答案 0 :(得分:6)

程序进入无限循环的原因是循环

while (n > 1)
    x = n * fact(n-1);

永不递减n。由于n永不减少,程序永远不会离开循环。 Peter在评论中是正确的:将while更改为if,您将拥有一个正确处理所有正参数的阶乘函数。但是,即使将while更改为if,您的fact也不会拥有fact(0) == 1的属性,这是正确的阶乘函数所必需的。

答案 1 :(得分:5)

while(n>1)

导致循环。你不要在循环中改变n,所以循环是无限的。

while更改为if

答案 2 :(得分:3)

这是阶乘的方法:

public int fact(int n)
    {
        if (n < 1)
        {
            return 1;
        }
        else
        {
            return n * fact(n - 1);
        }
    }

答案 3 :(得分:2)

#include <stdio.h>
#include <stdlib.h>

/** main returns int, use it! */

int main(int argc, char **argv)
{

if (argc <= 2) {
        if (argv) argc = atoi(argv[1] );
        else return argc;
        }

argc *= main (argc-1, NULL);

if (argv) {
        printf("=%d\n", argc);
        return 0;
        }
return argc;
}

答案 4 :(得分:2)

/*
Write a C++ Program to input a positive number,
Calculate and display factorial of this number
by recursion.
*/
#include<iostream.h>

#include<conio.h>

long  factorial(int n);
void main()
{

  clrscr();

  int number, counter;


  label1:

  cout<<"\n Enter the Number = ";

  cin>>number;

  if ( number < 0)
  {
  cout<<"\n Enter a non negative number, please!";
  goto label1;
  }
  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n The Factorial of the number "<<number<<"\n is "<<factorial(number);

  getch();

}

long factorial(int n)

{

    if ( n == 0 )
        return 1;
    else
        return n * factorial(n-1);

}

答案 5 :(得分:2)

您可以使用递归的简单方法

#include <stdio.h>

int fact(int n)
{
    if(n==1)
        return 1;
    else
        return n * fact(n-1);
}
int main()
{
    int f;
    f = fact(5);
    printf("Factorial = %d",f);
    return 0;
}

更多阅读C program to find factorial using recursion

答案 6 :(得分:1)

/*several versions of a factorial program.*/

#include<stdio.h>
int main()
  {
  int n;
  long factorial;
  printf("Compute the factorial of what number? ");
  scanf("%d", &n);
  factorial = 1L;
  while(n > 0)
    factorial *= n--;
  printf("The factorial is %ld\n", factorial);
  return 0;
  }

 #include<stdio.h>
/*the same, but counting up to n instead of down to 0*/
int main()
  {
  register int count;
  int n;
  long factorial;
  printf("Compute the factorial of what number? ");
  scanf("%d", &n);
  factorial = 1L;
  count = 1;
  while(count <= n)
    factorial *= count++;
  printf("%d! = %ld\n", n, factorial);
  return 0;
  }


 #include<stdio.h>
/*an equivalent loop using 'for' instead of 'while'*/
int main()
  {
  register int count;
  int n;
  long factorial;
  printf("Compute the factorial of what number? ");
  scanf("%d", &n);
  for(factorial = 1L, count = 1; count <= n; count++)
    factorial *= count;
  printf("%d! = %ld\n", n, factorial);
  return 0;
  }

答案 7 :(得分:0)

/*WAP to find factorial using recursion*/

#include<stdio.h>
#include<stdlib.h>

int fact1=1;

int fact(int no)
{
    fact1=fact1*no;
    no--;
    if(no!=1)
    {
        fact(no);
    }
    return fact1;
}

int main()
{
    int no,ans;``
    system("clear");
    printf("Enter a no. : ");
    scanf("%d",&no);
    ans=fact(no);
    printf("Fact : %d",ans);
    return 0;
}

答案 8 :(得分:0)

在C中使用while循环的递归程序。

int fact(int n)
{    
    int x=1;    

    while(n>=1)    
    {    
        return(n*fact(n-1));    
    }    

    return(1);    
}

答案 9 :(得分:0)

您可以使用这种方法。

int factorial(int a)
{
    while(a>1)
    {
        return a*factorial(a-1);
    }
    return 1;


}