C language: calculate the value of Polynomial 1-1/2 + 1/3-1/4 +... + 1/99-1/100, three types of cyclic implementation
Method 1: for Loop Implementation Program: # include <stdio. h> int main () {double I = 0, t = 0, sum = 0, sign =-1; for (I = 1; I <= 100; I ++) {sign =-sign; t = sign/I; sum = sum + t;} printf ("sum = % lf \ n", sum); return 0;} Method 2: while loop Implementation Program: # include <stdio. h> int main () {double I = 1, t = 0, sum = 0, sign =-1; while (I <= 100) {sign =-sign; t = sign/I; sum = sum + t; I ++;} printf ("sum = % lf \ n", sum); return 0;} method 3: do... while loop implementation # include <stdio. h> int main () {double I = 1, t = 0, sum = 0, sign =-1; do {sign =-sign; t = sign/I; sum = sum + t; I ++;} while (I <= 100); printf ("sum = % lf \ n", sum); return 0 ;}result: sum = 0.688172 press any key to continue...