此代码易学
#include<iostream>
using namespace std;
int fac(int);
int main()
{
int n;
while (cin >> n)
{
cout << n << "!= " << fac(n) << endl;
}
return 0;
}
int fac(int x)
{
register int i, f = 1;
for (i = 1; i <= x; i++)
f *= i;
return f;
}
下面这个较为高效。
#include <iostream>
using namespace std;
int main()
{
int i, n, factorial=1;
cout << "请输入数字:";
cin >> n;
for (i = 1; i <= n; i++)
{
factorial *= i;
}
cout << factorial << endl;
return 0;
}
来源:CSDN
作者:处女座绛翎儿
链接:https://blog.csdn.net/shnagmiao/article/details/104574178