C/C++ Programming Learning-Week 22① Factorial

Topic link

Title description

Given an integer n, find its factorial, n is less than or equal to 10.

Input
input a number n (1<=n<=10)

Output
outputs a number, representing the factorial of n

Sample Input

5

Sample Output

120

Ideas

Calculate the factorial, the factorial of n is to multiply from 1 to n.

C++ code:

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

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/113572514