2

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

10 に答える 10

6

プログラムが無限ループに陥る理由は、ループが

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

減少することはありませんnn減少しないため、プログラムがループから抜け出すことはありません。ピーターはコメントで正しいです: を に変更するwhileと、ifすべての正のパラメーターを正しく処理する階乗関数が得られます。ただし、 に変更した後でも、while正しい階乗関数に必要なというプロパティはありません。iffactfact(0) == 1

于 2012-02-11T19:29:25.087 に答える
5

これ

while(n>1)

ループを引き起こしています。ループ内で変更しないnため、ループは無限です。

に変更whileifます。

于 2012-02-11T19:29:10.633 に答える
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);

}
于 2013-12-11T10:02:45.273 に答える
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 プログラム

于 2015-04-28T03:12:33.083 に答える
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;
}
于 2012-07-11T09:31:35.240 に答える
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;
  }
于 2012-02-11T19:42:53.033 に答える
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;
}
于 2016-04-03T05:38:55.453 に答える