2012-02-11 100 views
2

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

#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);  
} 
+5

是否改变了一个如果解决了问题? – 2012-02-11 19:25:43

+1

当'n> 1'时,你的程序有一个无限循环,参见Peter的评论 – 2012-02-11 19:27:49

+0

顺便说一句,如果这是一个家庭作业问题,请用[tag:homework]标记它。 – Caleb 2012-02-11 19:30:58

回答

5

while(n>1) 

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

变化while变为if

6

,你的程序进入一个无限循环的原因是循环

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

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

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

这是阶乘的方法:

public int fact(int n) 
    { 
     if (n < 1) 
     { 
      return 1; 
     } 
     else 
     { 
      return n * fact(n - 1); 
     } 
    } 
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); 

} 
+0

OP要求C,而不是C++ – Mawg 2015-05-08 12:34:54

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

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