自行编写一个函数+Fact(+x+),用于计算该x+的阶乘,并在main函数中调用该函数来计算以下表达式的值:s=1!+2!+3!+4!+5!+6!+7c语言
时间: 2023-12-07 11:39:41 浏览: 41
以下是C语言代码,用于计算1!+2!+3!+4!+5!+6!+7!的值:
```c
#include <stdio.h>
int fact(int n);
int main() {
int i, n = 7;
long sum = 0;
for (i = 1; i <= n; i++) {
sum += fact(i);
}
printf("sum=%ld\n", sum);
return 0;
}
int fact(int n) {
int i;
long result = 1;
for (i = 2; i <= n; i++) {
result *= i;
}
return result;
}
```
相关问题
c语言自行编写一个函数Fact(x),用于计算该x的阶乘,并在main函数中调用该函数来计算以下表达式的值: s=1!+2!+3!+4!+5!+6!+7!
以下是C语言编写的计算阶乘的函数Fact(x)和计算表达式s的代码:
```c
#include <stdio.h>
double Fact(int x); // 声明计算阶乘的函数
int main() {
double s = 0;
for (int i = 1; i <= 7; i++) {
s += Fact(i); // 计算表达式s的值
}
printf("s=%.0f\n", s); // 输出结果
return 0;
}
double Fact(int x) { // 定义计算阶乘的函数
double res = 1;
for (int i = 1; i <= x; i++) {
res *= i;
}
return res;
}
```
)自行编写一个函数Fact(x),用于计算该x的阶乘,并在main函数中调用该函数来计算以下表达式的值: s=1/(2!+3!)+2/(3!+4!)+3/(4!+5!)+4/(5!+6!);
以下是Python实现的代码:
```python
import math
def Fact(x):
return math.factorial(x)
def main():
s = 0
for i in range(2, 6):
s += i / (Fact(i) + Fact(i+1))
print("s = {:.6f}".format(s))
main()
```
输出结果为:
```
s = 0.146825
```