code/old_homework/12.7_2.cpp

33 lines
537 B
C++
Raw Normal View History

2024-12-07 09:00:11 +08:00
//计算阶乘
2024-12-06 22:52:31 +08:00
#include <iostream>
using namespace std;
2024-12-07 09:00:11 +08:00
/*
2024-12-06 22:52:31 +08:00
int main() {
int n;
cin >> n; // 输入一个正整数n
long long result = 1; // 使用long long类型来避免结果溢出
// 计算阶乘
for (int i = 1; i <= n; ++i) {
result *= i;
}
cout << result << endl; // 输出阶乘的结果
return 0;
}
2024-12-07 09:00:11 +08:00
*/
int main(){
long long sum = 1;
int n;
cin >>n;
int i = 1;
while (i <= n)
{
sum *= i;
i++;
}
cout <<sum<<endl;
return 0;
}