简体   繁体   English

我的阶乘 function 没有返回阶乘

[英]My factorial function is not returning factorial

I am not able to find out why my function returns the user input only rather then the factorial of the input.我无法找出为什么我的 function 只返回用户输入而不是输入的阶乘。

#include <stdio.h>
#include <math.h>
int factorial(int x)
{
    //int x;
    int sum = 1;
    while (x!=0){
        sum = sum * x;
        x--;
    }
    return sum;
}

int main(){
    int x;
    printf("Enter value of x: ");
    scanf("%i",&x);
    factorial(x);
    printf("sum is %i", x);
    
    return 0;
}

Your factorial function does return a new value, but then you don't actually use that value.您的factorial function 确实返回了一个新值,但是您实际上并没有使用该值。

printf("sum is %i\n", factorial(x));

Because you are printing x which is the variable that you have stored the user input in. Your factorial function returns the result, but you are not saving it.因为您正在打印x这是您存储用户输入的变量。您的factorial function 返回结果,但您没有保存它。

I think the variable names were not proper and you printed x instead of printing factorial.我认为变量名称不正确,您打印了 x 而不是打印阶乘。

#include <stdio.h>
int factorial(int x)
{
int fact = 1;
while (x!=0){
    fact = fact * x;
    x--;
}
return fact;
}

int main(){
   int x;
   printf("Enter value of x: ");
   scanf("%i",&x);
   printf("Factorial is %i",factorial(x));
   return 0;
}

For starters the function as is对于初学者,function 原样

int factorial(int x)
{
    //int x;
    int sum = 1;
    while (x!=0){
        sum = sum * x;
        x--;
    }
    return sum;
}

can invoke undefined behavior if the user will pass a negative value, and the function accepts negative values.如果用户将传递负值,则可以调用未定义的行为,并且 function 接受负值。

The function argument should have an unsigned integer type instead of the signed integer type int function 参数应该有一个无符号的 integer 类型,而不是有符号的 integer 类型int

For non-negative values the maximum value of the types int or unsigned int for which the factorial can be calculated is equal to 12 .对于非负值,可以计算阶乘的intunsigned int类型的最大值等于12

So to be able to calculate the factorial for greater values you should use the type unsigned long long int .因此,为了能够计算更大值的阶乘,您应该使用类型unsigned long long int In this case the maximum value for which the factorial can be calculated correctly is equal to 20.在这种情况下,可以正确计算阶乘的最大值等于 20。

The function can look the following way function可以看如下方式

unsigned long long int factorial( unsigned long long int x )
{
    unsigned long long int product = 1;

    for ( ; 1 < x; --x )
    {
        product *= x;
    }

    return product;
}

In your program you are not using the returned value of the function.在您的程序中,您没有使用 function 的返回值。

factorial(x);

The function main can look the following way function主要可以看下面的方式

int main( void )
{
    unsigned int x;

    printf( "Enter value of x: " );

    if ( scanf( "%u",&x ) == 1 )
    {
        printf("The factorial of %u is equal to %llu\n, x, factorial( x ) );
    }
    
    return 0;
}

Now try the program by entering the value for x equal to 20 and see the program output.:)现在通过输入x等于20的值来尝试该程序,并查看程序 output。:)

You could check in the if statement that the user did not enter a value greater than 20 as for example您可以在 if 语句中检查用户没有输入大于 20 的值,例如

    if ( scanf( "%u",&x ) == 1 && !( 20 < x ) )

Though it would be better if the function itself will check the value of the passed argument.虽然如果 function 本身会检查传递参数的值会更好。

Failure to use function return value使用失败 function 返回值

As others have said:正如其他人所说:

//factorial(x);
//printf("sum is %i", x);
printf("sum is %i", factorial(x));

To improve factorial()改进factorial()

  • Since factorial is a product, change the sum name.由于阶乘是乘积,请更改sum名称。

  • Cope with pathologic inputs like values less than 0 or very large.应对病理输入,例如小于 0 或非常大的值。 Code code exit, but maybe instead return a error value.代码退出,但可能会返回错误值。 Determination of the upper limit could be done beforehand (as below), at run time or with makefile magic.可以预先确定上限(如下所示),在运行时或使用 makefile 魔法。

  • Use a wider type to handle large values.使用更宽的类型来处理大值。 Maybe even use an unsigned type.甚至可能使用无符号类型。 uintmax_t has the greatest max value. uintmax_t具有最大的最大值。 It is at least 64 bits.它至少为 64 位。

Example例子

#include <limits.h>
#include <stdint.h>

#if UINTMAX_MAX == 0xFFFFFFFFFFFFFFFFu
  #define FACTORIAL_MAX 20
#elif (UINTMAX_MAX >> 64) == 0xFFFFFFFFFFFFFFFFu
  #define FACTORIAL_MAX 34
#else
  #error TBD FACTORIAL_MAX
#endif

// Compute factorial.
// Return 0 on error.
uintmax_t factorial(int x) {
  if (x < 0 || x > FACTORIAL_MAX) {
    return 0;
  }
  uintmax_t product = 1;
  while (x > 0) {
    product = product * x;
    x--;
  }
  return product;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM