Python Program to Find the Sum of the Series: 1 + x^2/2 + x^3/3 + … x^n/n

This is a Python Program to find the sum of series: 1 + x^2/2 + x^3/3 + … x^n/n.

Problem Description

The program takes in the the number of terms and finds the sum of series: 1 + x^2/2 + x^3/3 + … x^n/n.

Problem Solution

1. Take in the number of terms to find the sum of the series for.
2. Initialize the sum variable to 0.
3. Use a for loop ranging from 1 to the number and find the sum of the series.
4. Print the sum of the series after rounding it off to two decimal places.
5. Exit.

Program/Source Code

Here is source code of the Python Program to find the sum of series: 1 + 1/2 + 1/3 + ….. + 1/N. The program output is also shown below.

n=int(input("Enter the number of terms:"))
x=int(input("Enter the value of x:"))
sum1=1
for i in range(2,n+1):
    sum1=sum1+((x**i)/i)
print("The sum of series is",round(sum1,2))
Program Explanation

1. User must enter the number of terms to find the sum of.
2. The sum variable is initialized to 0.
3. The for loop is used to find the sum of the series and the number is incremented for each iteration.
4. The numbers are added to the sum variable and this continues till the value of i reaches the number of terms.
5. Then the sum of the series is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter the number of terms:3
Enter the value of x:1
The sum of series is 1.83
 
Case 2:
Enter the number of terms:5
Enter the value of x:2
The sum of series is 16.07

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.