Sum of series 2 / 3 | C ++ program to find sum of given series ; Function to find sum of series up - to n terms ; initializing counter by 1 ; variable to calculate result ; while loop until nth term is not reached ; boolean type variable for checking validation ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double seriesSum ( int n ) { int 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 ; } } return res ; } int main ( ) { int n = 5 ; cout << seriesSum ( n ) ; return 0 ; }