Sum of Series||C++ language||programming_info ||Series problem 14

To calculate sum of the first N terms of the series:
2/3-4/5+6/7-8/9+.....

Solution using C++ language:

#include <iostream>
using namespace std;
int main() {
   int n = 17,i = 1;
   double res = 0.0;
   bool sign = true;
   while (n > 0) {
      n--;
      if (sign) {
         sign = !sign;
         res = res + (double)++i / ++i;
      } else {
         sign = !sign;
         res = res - (double)++i / ++i;
      }
   }
   cout << "The sum of the given series is "<< res;
   return 0;
}


Output:
The sum of given series is 0.77152


Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support