Howto compute the factorial of x

前端 未结 7 1386
一生所求
一生所求 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:06

    My code is not good as other but it works for me:

    #include 
    using namespace std;
    
    unsigned int fattoriale (int n){
        if (n == 1){
            return 1;
        }
        else {
            return n * fattoriale(n-1);
        }
    }
    
    int main() {
        int tmp, num;
        cin >> num;
    
        tmp = fattoriale(num);
    
        cout << "Stampo il fattoriale del numero inserito: " << tmp << endl;
    
    }
    

提交回复
热议问题