# Find sum of the series 1-2+3-4+5-6+7…….

Given a number N. The task is to find the sum of the below series up to n^{th} term.

1- 2 + 3 – 4 + 5 – 6 +….

**Examples**:

Input : N = 8 Output : -4 Input : N = 10001 Output : 5001

**Approach:** If we observe carefully, we can see that the sum of the above series follows a pattern of alternating positive and negative integers starting from 1 to N as shown below:

N = 1, 2, 3, 4, 5, 6, 7......Sum = 1, -1, 2, -2, 3, -3, 4......

Hence, from the above pattern, we can coonclude that:

- when n is odd => sum = (n+1)/2
- when n is even => sum = (-1)*n/2

Below is the implementation of the above approach:

## C++

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

*chevron_right*

*filter_none*

## Java

`// Java program to find sum of ` `// first n terms of the given series ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` `static` `int` `calculateSum(` `int` `n) ` `{ ` ` ` `// when n is odd ` ` ` `if` `(n % ` `2` `== ` `1` `) ` ` ` `return` `(n + ` `1` `) / ` `2` `; ` ` ` ` ` `// when n is not odd ` ` ` `return` `-n / ` `2` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String ar[]) ` `{ ` ` ` `// no. of terms to find the sum ` `int` `n = ` `8` `; ` `System.out.println(calculateSum(n)); ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python program to find the sum of ` `# series 1 - 2 + 3 - 4 +...... ` ` ` `# Function to calculate sum ` `def` `solve_sum(n): ` ` ` `# when n is odd ` ` ` `if` `(n ` `%` `2` `=` `=` `1` `): ` ` ` `return` `(n ` `+` `1` `)` `/` `2` ` ` ` ` `# when n is not odd ` ` ` `return` `-` `n ` `/` `2` ` ` `# Driver code ` `n ` `=` `8` ` ` `print` `(` `int` `(solve_sum(n))) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum of ` `// first n terms of the given series ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `calculateSum(` `int` `n) ` `{ ` ` ` `// when n is odd ` ` ` `if` `(n % 2 == 1) ` ` ` `return` `(n + 1) / 2; ` ` ` ` ` `// when n is not odd ` ` ` `return` `-n / 2; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` ` ` `// no. of terms to find the sum ` ` ` `int` `n = 8; ` ` ` `Console.WriteLine(calculateSum(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by inder_verma ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the sum of ` `// series 1 - 2 + 3 - 4 +...... ` ` ` `// Function to calculate sum ` `function` `solve_sum(` `$n` `) ` `{ ` ` ` `// when n is odd ` ` ` `if` `(` `$n` `% 2 == 1) ` ` ` `return` `(` `$n` `+ 1) / 2; ` ` ` ` ` `// when n is not odd ` ` ` `return` `-` `$n` `/ 2; ` `} ` ` ` `// Driver code ` `$n` `= 8; ` ` ` `echo` `solve_sum(` `$n` `); ` ` ` `// This code is contributed ` `// by inder_verma ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

-4

## Recommended Posts:

- Find the sum of series 3, 7, 13, 21, 31....
- Find the sum of the series x(x+y) + x^2(x^2+y^2) +x^3(x^3+y^3)+ ... + x^n(x^n+y^n)
- Find the sum of the series 2, 5, 13, 35, 97...
- Find the nth term of the given series
- Find n-th term of series 2, 10, 30, 68, 130 ...
- Find Nth term of the series 1, 5, 32, 288 ...
- Program to find the sum of a Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)
- Find the Nth term of the series 14, 28, 20, 40,.....
- Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Find Nth term of the series 1, 6, 18, 40, 75, ....
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n
- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)
- Find the sum of infinite series 1^2.x^0 + 2^2.x^1 + 3^2.x^2 + 4^2.x^3 +.......
- Find n-th term in the series 9, 33, 73,129 ...

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.