Write a Python script to find sum of the series :
s = 1 + x + x2 + x3 + …. + xn
Answers
Answered by
17
# python program to find the sum of series: 1 + x² + x³ + ... + xⁿ
n = int(input('Enter the value of n: '))
x = int(input('Enter the value of x: '))
sum_of_series = 0
for i in range(n+1):
sum_of_series += pow(x,i)
print('Sum of the series is:', sum_of_series)
- Input the value of x and n from user as integer type
- define a variable with value of zero to add up till the sum of series, this must be outside the loop, else each time the loop repeats value will set to zero defeating the purpose.
- we use for loop with range of i varying from 0 to n (remember this is not for infinite series) giving the power of x from 0 to n
- evey iteration we add x^i to the sum
- finally print the sum of series
Answered by
23
Python script to find sum of the series :
s = 1 + x + x^2 + x^3 + …. + x^n
where x and n are the the number input by user to form the user, x representing the base and n representing the power till the series would go.
x = float (input(“Enter value of x :”))
n= int (input (“Enter value of n :”))
s = 0
for i in range (n + 1) :
s+= x**i
print ("Sum of the series:”,s)
Similar questions