#include<stdio.h>#include<math.h>intmain(){int a;float c, s =0;for(a =1;; a +=2){if(a /2%2==0){
c =1./ a;}else
c =-1./ a;
s = s + c;if(abs(c)<0.000001){
s = s *4;break;}}printf("%f", s);return0;}
2.求e的近似值:e≈1+1/1!+1/2!+1/3!+…+1/n!,使误差小于10^-5。
#include<stdio.h>intmain(){int a, b =1, m;double c, s =1;for(a =1;; a++){for(m =1; m <= a; m++){
b = b * m;}
c =1./ b;
b =1;
s = s + c;if(c <0.00001){break;}}printf("%f", s);return0;}