2012-02-11 96 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; 
}