C++ 程序显示意外输出

Unexpected Output Shown by Program in C++

#include <iostream>

using namespace std;

void long_fctrl(int num[], int f_num) // Function for Factorial
   {
    --f_num; // Decrement Number to multiply with Factorial Number. 
    int i = 0, pow = 0, x, j, len = 0, k = 0;
    while(f_num > 1) // Loop for Factorial Number
    {
      i = 0;
          do { // Loop to Multiply Two Array Number
            x = (num[i]*f_num) + pow;
            num[i] = x%10;
            pow = x/10;
            ++i;
         }while((pow!=0) || (x == 0));
       --f_num;
      if(k == 0) //Condition to Find the Length of Factorial Number in array
      {
          len = i;
          k = 1;
      }
      if(i > len)// Condition for factorial array Length
        len = i;
    }
     cout << "Your Number : \n";
     for(j = len-1; j >= 0; --j) // Loop for Output of Factorial Number..
        cout << num[j];
}

int main()
{
    cout << "Enter Number (1 - 100) : ";
    int num, temp;
    cin >> num;// num is the number of which we have to calculate Factorial.
    if(num > 0 && num <= 100)
    {
        int fctrl[200] = {0};
        int i;
        temp = num;// Temp initialize to Factorial Number..
        for(i = 0; temp!= 0; ++i,temp/=10) 
        {
             fctrl[i] = temp%10;
        } // Loop to insert user entered Number in Factorial Array...
        long_fctrl(fctrl, num);  //Giving Factorial Number in Array and also
                                 //in the integer form to the Function 
    }
    return 0;
}  

程序是计算 1 到 100 范围内的阶乘 ..
通过输入小于 6 的数字,它给出了正确的输出
输入: 5
输出: 120
但对于其他数字,它给出了错误的输出
输入: 9
输出: 8080
预期输出: 362880
我找不到我的逻辑错误......................
如果您发现任何错误,请告诉我......
..................................................... ..........
..................................................... ............

如果您正在使用 C++ IDE 例如(代码块),您应该调试您的程序并逐步检查哪里出了问题。

你结束乘法循环的条件 (while((pow!=0) || (x == 0))) 是错误的。这个循环需要 运行 直到 i >= len, and pow != 0.

如果是9,你会得到

9
72    * 8
504   * 7
524   * 6

因为循环在 24 之后结束,因为 pow == 0 和 x == 2; 5是之前遗留下来的