Part of the series

Several example codes

~2 min read • Updated Sep 16, 2025

Program Overview

This Python program reads an angle x (in radians) and a number n representing the number of terms.
It calculates sin(x) using the Taylor series expansion:
sin(x) = x - x³/3! + x⁵/5! - x⁷/7! + ...
The more terms used, the more accurate the result.


Python Code:


def factorial(k):
    result = 1
    for i in range(1, k + 1):
        result *= i
    return result

def sine_taylor(x, n):
    sin_x = 0
    for i in range(n):
        sign = (-1) ** i
        term = sign * (x ** (2 * i + 1)) / factorial(2 * i + 1)
        sin_x += term
    return sin_x

x = float(input("Enter angle x (in radians): "))
n = int(input("Enter number of terms: "))

result = sine_taylor(x, n)
print("The sine of the angle is:", result)

Sample Output:


Enter angle x (in radians): 1.5708
Enter number of terms: 10
The sine of the angle is: 0.999999943741051

Explanation:

Here’s how the program works:
- The factorial() function computes factorial values
- The sine_taylor() function calculates sine using the Taylor series
- Each term alternates in sign and increases in power and factorial
- The result is printed using the print() function


Written & researched by Dr. Shahin Siami