The series:- I want to write a python program in which we can can input the the value of x and n and solve this series. can anyone help me please? The Series:- x-x^2 x^3/3-x^4/4 ...x^n/n
x = int (input ("Enter value of x: "))
numbed = int (input ("Enter value of n: "))
summed = 0
for a in range (numbed 1) :
if a%2==0:
summed = (x**a)/numbed
else:
summed -= (x**a)/numbed
print ("Sum of series", summed)
**I tried this code, but no matter what values I enter, the output is always 0.00. **
CodePudding user response:
This should do the trick:
x = int(input("Enter value of x: "))
n = int(input("Enter value of n: "))
total = x
for i in range(2, n 1):
if i%2==0:
total -= x**i/i
else:
total = x**i/i
print("Sum: ", total)
CodePudding user response:
I assume the series you mentioned in your question is this: x - x^2/2 x^3/3 - x^4/4 ... x^n/n.
If yes, try this:
x = int (input ("Enter value of x: "))
numbed = int (input ("Enter value of n: "))
sum1 = x
for i in range(2,numbed 1):
if i%2==0:
sum1=sum1-((x**i)/i)
else:
sum1=sum1 ((x**i)/i)
print("The sum of series is",round(sum1,2))
CodePudding user response:
just as an example of using recurcion:
x = int(input("Enter value of x: "))
n = int(input("Enter value of n: "))
def f(x,n,i=1,s=1):
return 0 if i>n else x**i/i*s f(x,n,i 1,-s)
f(3,5) # 35.85
CodePudding user response:
I have just tried this (removed a few spaces) and it gave me 813802.1 for x=5 and n=10. Could you give a few more details about what you are trying so I can try to spot what's going wrong for you?
x = int(input ("Enter value of x: "))
numbed = int(input ("Enter value of n: "))
summed = 0
for a in range (numbed 1):
if a%2==0:
summed = (x**a)/numbed
else:
summed -= (x**a)/numbed
print ("Sum of series", summed)