4.(编程)用C语言求1-1/2+1/3-1/4+……+1/99-1/100;

//代码:

注意:在除法出现时,注意变量的类型(一般情况下是浮点型)

 1 #include
 2 int main()
 3 {
 4     int s;
 5     double sum,t,d;
 6     sum=1.0;
 7     d=2.0;
 8     s=1;
 9 
10     while(d <= 100)
11     {
12         s=(-1)*s;
13         t=s*(1/d);
14         sum = sum + t;
15         d=d+1;
16     }
17     printf("%f\n",sum);
18     return 0;
19 }

 

转载于:https://www.cnblogs.com/rookieclimber/p/10786245.html

你可能感兴趣的:(4.(编程)用C语言求1-1/2+1/3-1/4+……+1/99-1/100;)