Articles

Program to find sum of series in Python example 1

Program to find sum of series in Python example 1


In the python programming article, we are going to learn

program to find sum of the below series in python.

1-2+3-4+5-6..... upto n terms

The program to find the sum of the following series:

The program is given below:

 

# Owner : TutorialsInhand Author : Devjeet Roy

n = int(input("Enter the value of n: "))

sum_of_series = 0

for i in range(1,n+1):
    sum_of_series = sum_of_series + ((-1) ** (i+1))*i

print("The sum of the series is:",sum_of_series)

The output of the program is given below:

 

PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter the value of n: 4
The sum of the series is: -2
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter the value of n: 6
The sum of the series is: -3

Few important tips about the program

1. For the given value n, input by the user, we find the exact sign of the number depending upon its value.

2. All even numbers carries negative sign, while all odd numbers are positive.

3. We find elements at each position and add them up to get the required sum about n terms in the series.

 

sum-of-series-1-in-python

 


Python Numbers Program

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Data Science Enthusiast
Page Views :    Published Date : Oct 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!