① 代码:
#include
using namespace std;
long fac(int n);
int main(int argc, char const *argv[])
{
int sum;
sum = 0;
for (int i = 1; i <= 6; ++i) {
sum += fac(i);
}
cout << "The sum of factorials from 1~6 is " << sum << endl;
return 0;
}
long fac(int n)
{
int product=1;
for (int i = 1; i <=n; ++i)
product *= i;
return product;
}
② 运行:
The sum of factorials from 1~6 is 873
用C行么..