24 lines
389 B
C++
24 lines
389 B
C++
|
// 编写一个函数,用于计算一个整数的阶乘,并返回结果。
|
||
|
#include <iostream>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
int factorial(int n)
|
||
|
{
|
||
|
int result = 1;
|
||
|
while (n)
|
||
|
{
|
||
|
result *= n;
|
||
|
n--;
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
int n;
|
||
|
cout << "请输入一个整数:";
|
||
|
cin >> n;
|
||
|
cout << n << "! = " << factorial(n) << endl;
|
||
|
return 0;
|
||
|
}
|