Find sum of the series 1 | C ++ program to find the sum of series 1 - 2 + 3 - 4 + ... ... ; Function to calculate sum ; when n is odd ; when n is not odd ; Driver code
#include <iostream> NEW_LINE using namespace std ; int solve_sum ( int n ) { if ( n % 2 == 1 ) return ( n + 1 ) / 2 ; return - n / 2 ; } int main ( ) { int n = 8 ; cout << solve_sum ( n ) ; return 0 ; }