good coders i need ur help plz. in my assignment there is this tricky question. write a program to calculate sum of the series: 1 - 1/2 + 1/3 -1/4 +..... -n until the term that is less than 0.0001(1/10000) is reached. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

good coders i need ur help plz. in my assignment there is this tricky question. write a program to calculate sum of the series: 1 - 1/2 + 1/3 -1/4 +..... -n until the term that is less than 0.0001(1/10000) is reached.

Loops

17th Aug 2016, 5:33 AM
Sifuku Bulelani
Sifuku Bulelani - avatar
3 Answers
0
double sum = 1.0; for( int n =2; n<=10000; n++) { if ( n % 2 == 0 ) sum -= (1.0/n); else sum += (1.0/n); } Console.WriteLine(sum);
17th Aug 2016, 7:06 AM
Amit Gupta
Amit Gupta - avatar
0
thank u boet u real heped me
17th Aug 2016, 2:12 PM
Sifuku Bulelani
Sifuku Bulelani - avatar
0
double sum = 0, prevSum = 0; int n = 1; do { prevSum = sum; if (n % 2 == 0) sum -= 1.0 / n; else sum += 1.0 / n; n++; } while (sum - prevSum >= 0.0001); Console.WriteLine(sum);
17th Aug 2016, 8:19 PM
Tomek Cymes
Tomek Cymes - avatar