求s=1+1(1+2)+1(1+2+3)

2020-01-07 19:45:29 浏览数 (2)

求s=1 1/(1 2) 1/(1 2 3)…. 1/(1 2 3…. n)的值 #include <stdio.h> float fun(int n) { int i,s1=0; float s=0.0; for(i=1;i<=n;i ) {s1=s1 i; /求每一项的分母/ s=s 1.0/s1; /求多项式的值/ } return s; } void main() { int n; float s; void NONO ( ); printf("nPlease enter N:"); scanf("%d", &n); s = fun(n); printf("the result is: %fn", s); NONO(); } void NONO ( ) {/ 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 / FILE fp, wf ; int i, n ; float s;

fp = fopen("in.dat","r") ; wf = fopen("out.dat","w") ; for(i = 0 ; i < 10 ; i ) { fscanf(fp, "%d", &n) ; s = fun(n) ; fprintf(wf, "%fn", s) ; } fclose(fp) ; fclose(wf) ; }

0 人点赞