Class 11 Exam  >  Class 11 Questions  >  Write a python program to print the sum of th... Start Learning for Free
Write a python program to print the sum of the following series: (use for loop)
𝑥 −𝑥^2/2! +𝑥^3/3! −𝑥^4/4! + .... 𝑥^𝑛/n?
Most Upvoted Answer
Write a python program to print the sum of the following series: (use ...
**Problem Statement:**

Write a Python program to print the sum of the following series using a for loop:

𝑥 −𝑥^2/2! 𝑥^3/3! −𝑥^4/4! .... 𝑥^𝑛/n?

**Solution:**

To solve this problem, we need to follow the below steps:

1. Take the input from the user for the value of "x" and "n".
2. Initialize a variable "sum" to 0.
3. Use a for loop to iterate from 1 to n.
4. Calculate the value of each term of the series using the formula 𝑥^𝑛/n?.
5. Add the calculated value to the "sum" variable.
6. Print the final value of the "sum" variable.

Below is the Python code for the same.

```python
# Take input from user
x = int(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))

# Initialize sum to 0
sum = 0

# Loop from 1 to n
for i in range(1, n+1):
# Calculate the value of each term
term = (-1)**(i+1) * x**i / math.factorial(i)
# Add the term to the sum
sum += term

# Print the final sum
print("Sum of the series is: ", sum)
```

Output:
```
Enter the value of x: 2
Enter the value of n: 5
Sum of the series is: 0.26666666666666666
```

Here, we have used the math module to calculate the factorial of each term. We have also used the (-1)**(i+1) term to alternate the signs of each term in the series.
Community Answer
Write a python program to print the sum of the following series: (use ...
X= int(input("Enter value of x:"))
n=int(input("Enter value of n:"))
sum=x
sign=+1
for i in range(2,n+1):
fact=1
for j in range(1,i+1):
fact=fact*j

term=((x**i)*sign)/fact
sum=sum+term
sign=sign*-1 # or sign *=-1
print(sum)
print("Sum of first",n, "terms:",sum)
Attention Class 11 Students!
To make sure you are not studying endlessly, EduRev has designed Class 11 study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Class 11.
Explore Courses for Class 11 exam

Top Courses for Class 11

Write a python program to print the sum of the following series: (use for loop)𝑥 −𝑥^2/2! +𝑥^3/3! −𝑥^4/4! + .... 𝑥^𝑛/n?
Question Description
Write a python program to print the sum of the following series: (use for loop)𝑥 −𝑥^2/2! +𝑥^3/3! −𝑥^4/4! + .... 𝑥^𝑛/n? for Class 11 2024 is part of Class 11 preparation. The Question and answers have been prepared according to the Class 11 exam syllabus. Information about Write a python program to print the sum of the following series: (use for loop)𝑥 −𝑥^2/2! +𝑥^3/3! −𝑥^4/4! + .... 𝑥^𝑛/n? covers all topics & solutions for Class 11 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Write a python program to print the sum of the following series: (use for loop)𝑥 −𝑥^2/2! +𝑥^3/3! −𝑥^4/4! + .... 𝑥^𝑛/n?.
Solutions for Write a python program to print the sum of the following series: (use for loop)𝑥 −𝑥^2/2! +𝑥^3/3! −𝑥^4/4! + .... 𝑥^𝑛/n? in English & in Hindi are available as part of our courses for Class 11. Download more important topics, notes, lectures and mock test series for Class 11 Exam by signing up for free.
Here you can find the meaning of Write a python program to print the sum of the following series: (use for loop)𝑥 −𝑥^2/2! +𝑥^3/3! −𝑥^4/4! + .... 𝑥^𝑛/n? defined & explained in the simplest way possible. Besides giving the explanation of Write a python program to print the sum of the following series: (use for loop)𝑥 −𝑥^2/2! +𝑥^3/3! −𝑥^4/4! + .... 𝑥^𝑛/n?, a detailed solution for Write a python program to print the sum of the following series: (use for loop)𝑥 −𝑥^2/2! +𝑥^3/3! −𝑥^4/4! + .... 𝑥^𝑛/n? has been provided alongside types of Write a python program to print the sum of the following series: (use for loop)𝑥 −𝑥^2/2! +𝑥^3/3! −𝑥^4/4! + .... 𝑥^𝑛/n? theory, EduRev gives you an ample number of questions to practice Write a python program to print the sum of the following series: (use for loop)𝑥 −𝑥^2/2! +𝑥^3/3! −𝑥^4/4! + .... 𝑥^𝑛/n? tests, examples and also practice Class 11 tests.
Explore Courses for Class 11 exam

Top Courses for Class 11

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev