Python Program to Find the Sum of Sine Series

This is a Python Program to find the sum of sine series.

Problem Description

The program takes in the the number of terms and finds the sum of sine series.

Problem Solution

1. Take in the value of x in degrees and the number of terms and store it in separate variables.
2. Pass these values to the sine function as arguments.
3. Define a sine function and using a for loop, first convert degrees to radians.
4. Then use the sine formula expansion and add each term to the sum variable.
5. Then print the final sum of the expansion.
6. Exit.

Program/Source Code

Here is source code of the Python Program to find the sum of sine series. The program output is also shown below.

import math
def sin(x,n):
    sine = 0
    for i in range(n):
        sign = (-1)**i
        pi=22/7
        y=x*(pi/180)
        sine = sine + ((y**(2.0*i+1))/math.factorial(2*i+1))*sign
    return sine
x=int(input("Enter the value of x in degrees:"))
n=int(input("Enter the number of terms:"))
print(round(sin(x,n),2))
Program Explanation

1. User must enter the value of x in degrees and the number of terms and store it in separate variables.
2. These values are passed to the sine functions as arguments.
3. A sine function is defined and a for loop is used convert degrees to radians and find the value of each term using the sine expansion formula.
4. Each term is added the sum variable.
5. This continues till the number of terms is equal to the number given by the user.
6. The total sum is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter the value of x in degrees:30
Enter the number of terms:10
0.5
 
Case 2:
Enter the value of x in degrees:45
Enter the number of terms:15
0.71

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.