使用函数实现求下列值++1!+1!+2!+1!+2!+3!+……+1!+2!+3!+……+n!用C语言
时间: 2023-12-07 14:38:58 浏览: 83
以下是使用函数实现求下列值1!+2!+3!+4!+…+10!的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
int Factor(int n) //计算n的阶乘
{
int result = 1;
for (int i = 1; i <= n; i += 1) {
result *= i;
}
return result;
}
int FactorSum(int n) //计算1!+2!+3!+...+n!
{
int sum = 0;
for (int i = 1; i <= n; i += 1) {
sum += Factor(i);
}
return sum;
}
int main()
{
int n = 10;
int result = FactorSum(n);
printf("1!+2!+3!+...+%d! = %d\n", n, result);
system("pause");
return 0;
}
```
相关问题
使用函数实现求下列值++1!+1!+2!+1!+2!+3!+……+1!+2!+3!+……+n!
以下是使用函数实现求下列值的代码:
```python
def Factor(n): #计算n的阶乘
result = 1
for i in range(1, n+1):
result *= i
return result
def FactorSum(n): #计算1!+2!+3!+...+n!的和
sum = 0
for i in range(1, n+1):
sum += Factor(i)
return sum
n = 5 #n为5
result = FactorSum(n) #调用函数计算结果
print(result) #输出结果
```
使用函数实现求下列值 1!+1!+2!+1!+2!+3!+……+1!+2!+3!+……+n!用C语言
以下是使用函数实现求下列值1!+1!+2!+1!+2!+3!+……+1!+2!+3!+……+n!的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
int Factor(int n) //计算n的阶乘
{
int result = 1;
for (int i = 1; i <= n; i += 1) {
result *= i;
}
return result;
}
int FactorSum(int n) //计算1!+2!+3!+...+n!
{
int sum = 0;
for (int i = 1; i <= n; i += 1) {
for (int j = 1; j <= i; j += 1) {
sum += Factor(j);
}
}
return sum;
}
int main() {
int n = 5; //n为5
int result = FactorSum(n);
printf("result = %d\n", result); //输出结果
system("pause");
return 0;
}
```
阅读全文