write a python program to input the value of x and n and print the sum of 1-x+x^2-x^3+x^4+...........x^n
Answers
Explanation:
Given an integer X, the task is to print the series and find the sum of the series
1 + x^{1} + x^{2} + x^{3} + x^{4} ... x^{N}
Examples :
Input: X = 2, N = 5
Output: Sum = 31
1 2 4 8 16
Input: X = 1, N = 10
Output: Sum = 10
1 1 1 1 1 1 1 1 1 1
Here's a Python program to input the value of x and n, and print the sum of the series 1-x+x^2-x^3+x^4+...........x^n:
x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
sum = 1 # initialize sum to 1
sign = -1 # initialize sign to -1
for i in range(1, n+1):
term = sign * (x ** i) # calculate each term in the series
sum += term # add the term to the sum
sign *= -1 # change the sign for the next term
print("The sum of the series is:", sum)
- In this program, we first take input from the user for the values of x and n. We then initialize the sum of the series to 1 and the sign of the first term to -1.
- We then use a for loop to iterate through each term in the series, up to the nth term.
- For each term, we calculate the value using the formula (-1)^i * x^i, where i is the index of the term.
- We then add the term to the sum, and change the sign of the next term by multiplying the sign by -1.
- Finally, we print out the sum of the series.
To know more:
https://brainly.in/question/31655938?referrer=searchResults
https://brainly.in/question/5558161?referrer=searchResults
#SPJ3