Part of the series

Several example codes

~2 min read • Updated Oct 12, 2025

Program Overview

This Python program reads two values x and n and computes the result of the following trigonometric series:
∑ [tan⁽ᵏ⁾(x) / sin(x⁽ⁿ⁻ᵏ⁺¹⁾)] for k from 1 to n
The angle x is converted to radians, and math.tan and math.sin are used for calculations.


Python Code:


import math

def trig_series(x_deg: float, n: int) -> float:
    x_rad = math.radians(x_deg)
    total = 0.0
    for k in range(1, n + 1):
        numerator = math.tan(x_rad) ** k
        denominator = math.sin(x_rad ** (n - k + 1))
        if denominator == 0:
            raise ValueError(f"Division by zero at term k={k}")
        total += numerator / denominator
    return total

# Read inputs from user
x = float(input("Enter value for x (in degrees): "))
n = int(input("Enter value for n: "))

try:
    result = trig_series(x, n)
    print(f"Series result is: {result:.4f}")
except ValueError as e:
    print(f"Error in calculation: {e}")

Sample Output (input: x = 45, n = 3):


Series result is: 3.0463

Written & researched by Dr. Shahin Siami