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.
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)
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.