Write a program to compute sin x for given x. The user should supply x and a positive integer n.
We compute the sine of x using the series and the computation should use all terms in the series up through the term involving x
n
sin x = x - x
3
/3! + x
5
/5! - x
7
/7! + x
9
/9! ........
Source Code
x = int(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
sign = -1
fact = i =1
sum = 0
while i<=n:
p = 1
fact = 1
for j in range(1,i+1):
p = p*x
fact = fact*j
sign = -1*sign
sum = sum + sign* p/fact
i = i+2
print("sin(",x,") =",sum)
Output
Enter the value of x: 2
Enter the value of n: 3
sin( 2 ) = 0.6666666666666667