简体   繁体   English

为什么我的 FirstFactorial 程序即使在不满足条件后仍然循环回到 while 条件

[英]Why does my FirstFactorial program keep looping back to while condition even after the condition is not met

Here's the code snippet, this when run with number 4 outputs 2424242448484848288288288288576576576576 .这是代码片段,当运行数字4输出2424242448484848288288288288576576576576 Not sure as to why would the execution would jump back to while loop after exiting the function code.不确定为什么在退出函数代码后执行会跳回 while 循环。 Any help will be appreciated.任何帮助将不胜感激。 Thank you in advance.先感谢您。

#include <stdio.h>
#include <string.h>

int result = 1;
void FirstFactorial(int);

void FirstFactorial(int num) {
    // code goes here
    while (num > 0) {
        result = result * num;
        num--;
        FirstFactorial(num);
    }
    printf("%d", result);
}

int main(void) {
    int var;
    // keep this function call here
    printf ("Enter your no.\n");
    scanf("%d", &var);
    FirstFactorial(var);
    return 0;
}

Within the function函数内

void
FirstFactorial(int num)
{
  // code goes here
  while(num > 0)
    {
      result = result * num;
      num--;
      FirstFactorial(num);
    }
  printf("%d", result);
}

each its iteration calls itself num times and all iterations together output the global variable result.每个迭代调用自己num次,所有迭代一起输出全局变量结果。

So for example in the first call of the function the function calls itself in the while loop for the range of values [num, 1].因此,例如,在函数的第一次调用中,该函数在值 [num, 1] 的范围内的 while 循环中调用自身。

Remove the while loop and do not use the global variable.删除 while 循环,不要使用全局变量。

Here is a demonstrative program.这是一个演示程序。

#include <stdio.h>

unsigned long long int factorial( unsigned long long int n )
{
    return n < 2 ? 1 : n * factorial( n - 1 );
}

int main(void) 
{
    printf( "%llu! = %llu\n", 4llu, factorial( 4 ) );
    printf( "%llu! = %llu\n", 20llu, factorial( 20 ) );

    return 0;
}

The program output is程序输出是

4! = 24
20! = 2432902008176640000

Pay attention that the maximum value you may specify is 20.请注意,您可以指定的最大值为 20。

Either you implement the factorial with a loop or you do it recursively.要么使用循环实现阶乘,要么递归执行。

Both ways are feasible but your code mixes it up.两种方式都是可行的,但您的代码将其混淆。

Your function mixes iterative and recursive approaches.您的函数混合了迭代和递归方法。 You can correct it by removing the useless recursion which causes multiple intermediary results to be computed and printed.您可以通过删除导致计算和打印多个中间结果的无用递归来纠正它。 Defining result as a global variable is also a mistake, especially since you do not reinitialize it before the loop.result定义为全局变量也是一个错误,特别是因为您没有在循环之前重新初始化它。 Using type long long will allow for larger factorials to be computed.使用long long类型将允许计算更大的阶乘。 Adding a trailing \\n after the printf conversion specifier is advisable too.printf转换说明符之后添加尾随\\n也是可取的。

Here is a corrected version:这是一个更正的版本:

#include <stdio.h>

void FirstFactorial(int num) {
    long long result = 1;

    while (num > 1) {
        result = result * num;
        num--;
    }
    printf("%lld\n", result);
}

int main(void) {
    int var;
    // keep this function call here
    printf("Enter your number\n");
    if (scanf("%d", &var) == 1)
        FirstFactorial(var);
    return 0;
}

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

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