Skip to content
Related Articles

Related Articles

Sum of first N natural numbers with alternate signs
  • Last Updated : 15 Dec, 2020

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:
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 signs
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 Code
int main()
{
 
    int N = 6;
    cout<<alternatingSumOfFirst_N(N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find the sum of first
// N natural numbers with alternate signs
static 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 Code
public static void main(String[] args)
{
    int N = 6;
     
    System.out.print(alternatingSumOfFirst_N(N));
}
}
 
// This code is contributed by Amit Katiyar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
 
# Function to find the sum of
# First N natural numbers with
# alternate signs
def 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_

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to find the sum of first
// N natural numbers with alternate signs
static 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 Code
public static void Main(String[] args)
{
    int N = 6;
     
    Console.Write(alternatingSumOfFirst_N(N));
}
}
 
// This code is contributed by 29AjayKumar

chevron_right


Output: 

-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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 signs
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 Code
int main()
{
 
    int N = 6;
    cout<<alternatingSumOfFirst_N(N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*;
 
class GFG
{
 
// Function to find the sum of first
// N natural numbers with alternate signs
static 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 Code
public static void main(String[] args)
{
    int N = 6;
    System.out.print(alternatingSumOfFirst_N(N));
}
}
 
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to implement
# the above approach
 
# Function to find the sum of first
# N natural numbers with alternate signs
def 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 Code
if __name__ == '__main__':
    N = 6;
    print(alternatingSumOfFirst_N(N));
 
# This code contributed by shikhasingrajput

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach
using System;
class GFG
{
 
// Function to find the sum of first
// N natural numbers with alternate signs
static 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 Code
public static void Main(String[] args)
{
    int N = 6;
    Console.Write(alternatingSumOfFirst_N(N));
}
}
 
// This code is contributed by 29AjayKumar

chevron_right


Output: 

-3

 

Time Complexity: O(1) 
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :