printf(\); scanf(\ sum = 0; for(i = m; i <= n; i++)
sum = sum + i * i + 1.0 / i; printf(\
return 0; }
4.求交错序列前n项和:输入一个正整数 n,计算交错序列1-2/3+3/5-4/7+5/9-6/11+…的前n项之和。试编写相应程序。 解答:
#include
int flag, i, n; double item, sum;
printf(“ Input n:”); scanf(\ sum = 0; flag = 1;
for(i = 1; i <= n; i++){
item = flag * 1.0 * i / (2 * i - 1); sum = sum + item; flag = -flag; }
printf(\
return 0; }
h.h,并调用sqrt( ) 函数求平方根。试编写相应程序。 解答:
#include
int i, n; double sum;
printf(\ scanf(\ sum = 0;
for(i = 1; i <= n; i++){
sum = sum + sqrt(ii); }
printf(\
return 0;
}
6.求给定序列前n项和(1!+2!+…):输入一个正整数 n,求e=1!+2!3!+…+n!的值。要求定义和调用fact(n)计算n!,函数类型是 double。试编写相应程序。 解答:
#include
int i, n; double sum;
printf(\ scanf(\ sum = 0;
for(i = 0; i <= n; i++)
sum = sum + fact(i); printf(\
return 0;
}
double fact(int n) {
int i;
double product;
product=1;
for(i = 1; i <= n; i++)
product = product * i; return product; }