# 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.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://btechgeeks.com/python-program-to-find-sum-of-series-1-2-3-4-5-n/", "fetch_time": 1725838493000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-38/segments/1725700651035.2/warc/CC-MAIN-20240908213138-20240909003138-00356.warc.gz", "warc_record_offset": 137577247, "warc_record_length": 15368, "token_count": 1217, "char_count": 4633, "score": 3.53125, "int_score": 4, "crawl": "CC-MAIN-2024-38", "snapshot_type": "latest", "language": "en", "language_score": 0.7157489061355591, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00037-of-00064.parquet"}