Find the Sum of the series 1 / 2 | C ++ program for the above approach ; Function to find the sum of series ; Generate the ith term and add it to the sum if i is even and subtract if i is odd ; Print the sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSeriesSum ( int N ) { double sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i & 1 ) { sum += ( double ) i / ( i + 1 ) ; } else { sum -= ( double ) i / ( i + 1 ) ; } } cout << sum << endl ; } int main ( ) { int N = 10 ; printSeriesSum ( N ) ; return 0 ; }