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

22 Mar, 2021
Given a number N. The task is to find the sum of the below series up to nth 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++
Java
Python 3
C#
PHP
Javascript
// C++ program to find the sum of// series 1 - 2 + 3 - 4 +...... #include <iostream>using namespace std; // Function to calculate sumint 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 codeint main(){    int n = 8;         cout << solve_sum(n);         return 0;}
// Java program to find sum of// first n terms of the given seriesimport 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 codepublic static void main(String ar[]){ // no. of terms to find the sumint n = 8;System.out.println(calculateSum(n));}}
# Python program to find the sum of# series 1 - 2 + 3 - 4 +...... # Function to calculate sumdef solve_sum(n):    # when n is odd    if(n % 2 == 1):        return (n + 1)/2     # when n is not odd    return -n / 2 # Driver coden = 8 print(int(solve_sum(n)))
// C# program to find sum of// first n terms of the given seriesusing 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 codepublic static void Main(){     // no. of terms to find the sum    int n = 8;    Console.WriteLine(calculateSum(n));}} // This code is contributed// by inder_verma
<?php// PHP program to find the sum of// series 1 - 2 + 3 - 4 +...... // Function to calculate sumfunction 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?>
<script> // javascript program to find sum of// first n terms of the given series   function calculateSum(n){    // when n is odd    if (n % 2 == 1)        return (n + 1) / 2;     // when n is not odd    return -n / 2;} // Driver code // no. of terms to find the sumvar n = 8;document.write(calculateSum(n));  // This code contributed by shikhasingrajput </script>
-4
 
SURENDRA_GANGWAR
inderDuMCA
shikhasingrajput
series
Mathematical
School Programming
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Print all possible combinations of r elements in a given array of size n
Operators in C / C++
Program for factorial of a number
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects