计算编写c语言计算n!并调用该函数计算s=1+1/(1+4!)+1/(1+4!+7!)+…+1/(1+4!+7!+…+19!)
时间: 2023-11-24 11:51:28 浏览: 190
以下是C语言计算n!并调用该函数计算s=1+1/(1+4!)+1/(1+4!+7!)+…+1/(1+4!+7!+…+19!)的代码实现:
```c
#include <stdio.h>
double factorial(int n) {
double result = 1.0;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
double calculateS() {
double s = 0.0;
int n = 1;
double denominator = 1.0;
while (n <= 5) {
denominator *= factorial(3 * n - 2);
s += 1.0 / (1 + denominator);
n++;
}
return s;
}
int main() {
double s = calculateS();
printf("s is %f\n", s);
return 0;
}
```
相关问题
编写C语言函数计算N!,调用该函数计算下式的值: 变量 S=1+1/(1+4!)+1/(1+4!+7!)+···+1/(1+4!+7!+…+19!)
以下是C语言函数计算N!的代码和调用该函数计算S的代码:
```c
#include <stdio.h>
int fac(int n) // 阶乘函数
{
int i = 0;
int y = 1; // 用来存返回值
if (0 == n || 1 == n) {
return 1;
} else {
for (i = 2; i <= n; i++) {
y *= i;
}
return y;
}
}
double calc_S() // 计算S的函数
{
int i = 0, j = 0; // 循环变量
double S = 0; // 和
for (i = 0; i < 5; i++) { // 一共有5项
int n = 1 + i * 3; // 每一项的n
int f = fac(n); // 每一项的n!
int denominator = 1; // 每一项的分母
for (j = i; j >= 0; j--) { // 计算每一项的分母
denominator += 3 * j;
}
S += 1.0 / (denominator * f); // 计算每一项的值并加到和上
}
return S;
}
int main()
{
printf("S = %lf\n", calc_S());
return 0;
}
```
编写C语言函数计算N!,调用该函数计算下式的值:变量S=1+1/(1+4!)+1/(1+4!+7!)+···+1/(1+4!+7!+…+19!)(提示)定义函数+long+jc(int+k)用于求阶乘
以下是C语言函数计算N!的代码和调用该函数计算S的代码:
```c
#include <stdio.h>
long jc(int k) // 求阶乘函数
{
int i;
long result = 1;
for (i = 1; i <= k; i++) {
result *= i;
}
return result;
}
int main()
{
int i, n = 5; // n为需要计算的N的值
double S = 0;
for (i = 0; i < 5; i++) {
S += 1.0 / jc(1 + i * 3);
}
printf("S = %lf\n", S);
return 0;
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)