#include
#include
int main(){
unsigned long i, n;
double s, t;
printf("Input n:");
scanf("%ld",&n);
for(i=1, s=0; i<=n; ++i){
t=(i%2 ? 1.0 : -1.0)/i;
s+=t;
}
printf("s=%.14lf\n",s);
printf("\nFinished.\n");
getch();
return 0;
}
在TurboC++3.0中编译运行了,输入n=1000000,输出s=0.69314668056025,n=1000000000时,输出s=0.69314718006071,而ln(2)=0.69314718055995,可见极限值为ln(2)
//#include "stdafx.h"//vc++6.0加上这一行.
#include "stdio.h"
void main(void){
int i;
double sum;
for(sum=0,i=1;i<100000000;i+=2)
sum+=1.0/i*1.0/(i+1);
printf("%.10f\n",sum);
}
验证结果收殓于ln2!