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

问题描述 投票:2回答:10

使用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);    
}
c recursion factorial
10个回答
6
投票

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

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

永远不会减少n。由于n从未减少,程序将永远不会离开循环。彼得在评论中是正确的:将while更改为if,您将拥有一个正确处理所有正参数的阶乘函数。然而,即使将while改为if之后,你的fact将不具备fact(0) == 1的属性,正如所需的正确因子函数所需。


0
投票

你可以使用这种方法。

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


}

5
投票

这个

while(n>1)

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

while改为if


3
投票

这是阶乘的方法:

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

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;
}

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);

}

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


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;
  }

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;
}

0
投票

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

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

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

    return(1);    
}
© www.soinside.com 2019 - 2024. All rights reserved.