# Python Program to Find Sum of Series 1-2+3-4+5…+N

In the previous article, we have discussed Python Program to Find Sum of Series 1+1/3+1/5+1/7+…..1/(N+2)

Given a number N and the task is to find the sum of the given series (1-2+3-4+5…+N) for the given number in Python.

Examples:

Example1:

Input:

Given Number = 20

Output:

The total sum of the series till the given number { 20 } =  -10

Example2:

Input:

Given Number = 7

Output:

The total sum of the series till the given number { 7 } =  4

## Program to Find Sum of Series 1-2+3-4+5…+N in Python

Below are the ways to find the sum of the given series (1-2+3-4+5…+N) for the given number in Python:

### Method #1: Using For Loop (Static Input)

Approach:

• Give the number N as static input and store it in a variable.
• Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
• Loop from 1 to the given number using the for loop.
• Inside the loop, check if the value of iterator modulus 2 is equal to zero(even condition) or not using the if conditional statement.
• If the statement is True, then subtract the iterator value from the above-declared resltsum.
• Store it in the same variable resltsum.
• Else if the statement is False, then add the iterator value to the above-declared resltsum.
• Store it in the same variable resltsum.
• Print the resltsum value which is the result of the series till the given Number N.
• The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 20
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Loop from 1 to the given number using the for loop.
for itr in range(1, gvn_numb+1):
# Inside the loop, check if the value of iterator modulus 2 is equal to zero(even condition)
# or not using the if conditional statement.
if itr % 2 == 0:
# If the statement is True, then subtract the iterator value from the above-declared
# resltsum.
# Store it in the same variable resltsum.
resltsum -= itr
else:
# Else if the statement is False, then add the iterator value to the above-declared
# resltsum.
# Store it in the same variable resltsum.
resltsum += itr
# Print the resltsum value which is the result of the series till the given Number N.
print(
"The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

The total sum of the series till the given number { 20 } =  -10

### Method #2: Using For loop (User Input)

Approach:

• Give the number N as user input using the int(input()) function and store it in a variable.
• Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
• Loop from 1 to the given number using the for loop.
• Inside the loop, check if the value of iterator modulus 2 is equal to zero(even condition) or not using the if conditional statement.
• If the statement is True, then subtract the iterator value from the above-declared resltsum.
• Store it in the same variable resltsum.
• Else if the statement is False, then add the iterator value to the above-declared resltsum.
• Store it in the same variable resltsum.
• Print the resltsum value which is the result of the series till the given Number N.
• The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function and
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Loop from 1 to the given number using the for loop.
for itr in range(1, gvn_numb+1):
# Inside the loop, check if the value of iterator modulus 2 is equal to zero(even condition)
# or not using the if conditional statement.
if itr % 2 == 0:
# If the statement is True, then subtract the iterator value from the above-declared
# resltsum.
# Store it in the same variable resltsum.
resltsum -= itr
else:
# Else if the statement is False, then add the iterator value to the above-declared
# resltsum.
# Store it in the same variable resltsum.
resltsum += itr
# Print the resltsum value which is the result of the series till the given Number N.
print(
"The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

Enter some Random Number = 7
The total sum of the series till the given number { 7 } = 4

Find a comprehensive collection of Examples of Python Programs ranging from simple ones to complex ones to guide you throughout your coding journey.