为此,我们将使用 x 和 n 的值。我们的任务将是计算给定级数的和,至给定的 n 项。这可以通过计算阶乘并使用标准的幂函数来轻松完成。
示例
#include <math.h>
#include <stdio.h>
//calculating the sum of series
double calc_sum(double x, int n){
double sum = 1, term = 1, fct, j, y = 2, m;
int i;
for (i = 1; i < n; i++) {
fct = 1;
for (j = 1; j <= y; j++) {
fct = fct * j;
}
term = term * (-1);
m = term * pow(x, y) / fct;
sum = sum + m;
y += 2;
}
return sum;
}
int main(){
double x = 5;
int n = 7;
printf("%.4f", calc_sum(x, n));
return 0;
}