Howto compute the factorial of x

前端 未结 7 1401
一生所求
一生所求 2020-12-20 00:05

how to get the value of an integer x, indicated by x!, it is the product of the numbers 1 to x.

Example: 5! 1x2x3x4x5 = 120.

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-20 00:14

    int factorial(int x)
    {
        int f;
        if (x == 0)
        {
            f = 1;
        }
        else if (x > 0)
        {
         f = x*factorial(x-1);
        }
        return f;
    }
    
    int main()
    {
       int n = 0;
       cout << factorial(n);
    
       return 0;
    }
    

提交回复
热议问题