Luogu brush questions C++ language | P5739 Calculating factorial

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


[Title description]

Find  n !, which is 1×2×3⋯× n .

Challenge: Try to complete this task without using loop statements (for, while).

【enter】

The first line inputs a positive integer  n .

【Output】

Output a positive integer denoting  n !.

【Input sample】

3

【Output sample】

6

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;
int fun(int n) {
    if (n==1) {
        return 1;
    } else {
        return n * fun(n-1);
    }
}
int main()
{
    int n, ans=1;
    cin >> n;
    ans = fun(n);
    cout << ans;
    return 0;
}

【operation result】

3 
6

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132800873