Sum of first N natural numbers with alternate signs

14 Apr, 2021
Given an integer N, the task is to find the sum of first N natural numbers with alternate signs, i.e 1 – 2 + 3 – 4 + 5 – 6 + .... 
Examples:
Input: N = 6 Output: -3 Explanation: 1 – 2 + 3 – 4 + 5 – 6 = -3 Therefore, the required output is -3.
Input: N = 5 Output: 3 Explanation: 1 – 2 + 3 – 4 + 5 = 3 Therefore, the required output = 3
Naive Approach: Follow the steps below to solve the problem:
Initialize a variable, say alternateSum to store the sum of alternate sign of first N natural numbers.
Iterate over the range [1, N] using variable i and check if i is even or not. If found to be true then update alternateSum += -i.
Otherwise, update alternateSum += i.
Finally, print the value of alternateSum.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program to implement// the above approach #include <bits/stdc++.h>using namespace std; // Function to find the sum of first// N natural numbers with alternate signsint alternatingSumOfFirst_N(int N){    // Stores sum of alternate sign    // of first N natural numbers    int alternateSum = 0;         for (int i = 1; i <= N; i++) {         // If is an even number        if (i % 2 == 0) {             // Update alternateSum            alternateSum += -i;        }         // If i is an odd number        else {             // Update alternateSum            alternateSum += i;        }    }    return alternateSum;     } // Driver Codeint main(){     int N = 6;    cout<<alternatingSumOfFirst_N(N);    return 0;}
// Java program to implement// the above approachimport java.util.*; class GFG{ // Function to find the sum of first// N natural numbers with alternate signsstatic int alternatingSumOfFirst_N(int N){         // Stores sum of alternate sign    // of first N natural numbers    int alternateSum = 0;         for(int i = 1; i <= N; i++)    {                 // If is an even number        if (i % 2 == 0)        {                         // Update alternateSum            alternateSum += -i;        }         // If i is an odd number        else        {                         // Update alternateSum            alternateSum += i;        }    }    return alternateSum;} // Driver Codepublic static void main(String[] args){    int N = 6;         System.out.print(alternatingSumOfFirst_N(N));}} // This code is contributed by Amit Katiyar
# Python3 program to implement# the above approach # Function to find the sum of# First N natural numbers with# alternate signsdef alternatingSumOfFirst_N(N):         # Stores sum of alternate sign    # of First N natural numbers    alternateSum = 0         for i in range(1, N + 1):                 # If is an even number        if (i % 2 == 0):                         # Update alternateSum            alternateSum += -i                     # If i is an odd number        else:            alternateSum += i     return alternateSum # Driver Code if __name__ == "__main__" :         N = 6         print(alternatingSumOfFirst_N(N))        # This code is contributed by Virusbuddah_
// C# program to implement// the above approachusing System;class GFG{ // Function to find the sum of first// N natural numbers with alternate signsstatic int alternatingSumOfFirst_N(int N){         // Stores sum of alternate sign    // of first N natural numbers    int alternateSum = 0;         for(int i = 1; i <= N; i++)    {                 // If is an even number        if (i % 2 == 0)        {                         // Update alternateSum            alternateSum += -i;        }         // If i is an odd number        else        {                         // Update alternateSum            alternateSum += i;        }    }    return alternateSum;} // Driver Codepublic static void Main(String[] args){    int N = 6;         Console.Write(alternatingSumOfFirst_N(N));}} // This code is contributed by 29AjayKumar
<script> // Javascript program to implement// the above approach     // Function to find the sum of first    // N natural numbers with alternate signs    function alternatingSumOfFirst_N(N)    {         // Stores sum of alternate sign        // of first N natural numbers        var alternateSum = 0;         for (i = 1; i <= N; i++) {             // If is an even number            if (i % 2 == 0) {                 // Update alternateSum                alternateSum += -i;            }             // If i is an odd number            else {                 // Update alternateSum                alternateSum += i;            }        }        return alternateSum;    }     // Driver Code             var N = 6;         document.write(alternatingSumOfFirst_N(N)); // This code is contributed by Rajput-Ji </script>
-3
 
Time Complexity: O(N) Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the following observations:
If N is an even number then the sum of alternate sign of first N natural numbers are = (-N) / 2.
If N is an odd number then the sum of alternate sign of first N natural numbers are = (N + 1) / 2.
Follow the steps below to solve the problem:
Initialize a variable, say alternateSum to store the sum of alternate sign of first N natural numbers.
Check if N is an even number or not. If found to be true then update alternateSum = (-N) / 2.
Otherwise, update alternateSum = (N + 1) / 2.
Finally, print the value of alternateSum.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std; // Function to find the sum of first// N natural numbers with alternate signsint alternatingSumOfFirst_N(int N){    // Stores sum of alternate sign    // of first N natural numbers    int alternateSum = 0;     // If N is an even number    if (N % 2 == 0) {         // Update alternateSum        alternateSum = (-N) / 2;    }     // If N is an odd number    else {        // Update alternateSum        alternateSum = (N + 1) / 2;    }    return alternateSum;} // Driver Codeint main(){     int N = 6;    cout<<alternatingSumOfFirst_N(N);    return 0;}
// Java program to implement// the above approachimport java.util.*; class GFG{ // Function to find the sum of first// N natural numbers with alternate signsstatic int alternatingSumOfFirst_N(int N){    // Stores sum of alternate sign    // of first N natural numbers    int alternateSum = 0;     // If N is an even number    if (N % 2 == 0)    {         // Update alternateSum        alternateSum = (-N) / 2;    }     // If N is an odd number    else    {               // Update alternateSum        alternateSum = (N + 1) / 2;    }    return alternateSum;} // Driver Codepublic static void main(String[] args){    int N = 6;    System.out.print(alternatingSumOfFirst_N(N));}} // This code is contributed by 29AjayKumar
# Python program to implement# the above approach # Function to find the sum of first# N natural numbers with alternate signsdef alternatingSumOfFirst_N(N):       # Stores sum of alternate sign    # of first N natural numbers    alternateSum = 0;     # If N is an even number    if (N % 2 == 0):         # Update alternateSum        alternateSum = (-N) // 2;      # If N is an odd number    else:         # Update alternateSum        alternateSum = (N + 1) // 2;     return alternateSum; # Driver Codeif __name__ == '__main__':    N = 6;    print(alternatingSumOfFirst_N(N)); # This code contributed by shikhasingrajput
// C# program to implement// the above approachusing System;class GFG{ // Function to find the sum of first// N natural numbers with alternate signsstatic int alternatingSumOfFirst_N(int N){    // Stores sum of alternate sign    // of first N natural numbers    int alternateSum = 0;     // If N is an even number    if (N % 2 == 0)    {         // Update alternateSum        alternateSum = (-N) / 2;    }     // If N is an odd number    else    {               // Update alternateSum        alternateSum = (N + 1) / 2;    }    return alternateSum;} // Driver Codepublic static void Main(String[] args){    int N = 6;    Console.Write(alternatingSumOfFirst_N(N));}} // This code is contributed by 29AjayKumar
<script> // Javascript program to implement// the above approach     // Function to find the sum of first    // N natural numbers with alternate signs    function alternatingSumOfFirst_N(N)    {        // Stores sum of alternate sign        // of first N natural numbers        var alternateSum = 0;         // If N is an even number        if (N % 2 == 0) {             // Update alternateSum            alternateSum = (-N) / 2;        }         // If N is an odd number        else {             // Update alternateSum            alternateSum = (N + 1) / 2;        }        return alternateSum;    }     // Driver Code             var N = 6;        document.write(alternatingSumOfFirst_N(N)); // This code contributed by Rajput-Ji </script>
-3
 
Time Complexity: O(1) Auxiliary Space: O(1)
virusbuddha
29AjayKumar
amit143katiyar
shikhasingrajput
Rajput-Ji
Natural Numbers
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.