qfedu-basic-level/day9/homework/h7.cpp

24 lines
389 B
C++
Raw Normal View History

2023-06-26 20:46:20 +08:00
// 编写一个函数,用于计算一个整数的阶乘,并返回结果。
#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;
}