Part of the series

Several example codes

~1 min read • Updated Oct 12, 2025

Program Overview

This Python program reads two triangle sides B and C and the angle α between them.
It calculates the third side A using the Law of Cosines:
A² = B² + C² - 2 × B × C × cos(α)
The angle must be converted to radians before applying cos.


Python Code:


import math

def third_side(B: float, C: float, alpha_deg: float) -> float:
    alpha_rad = math.radians(alpha_deg)
    A_squared = B**2 + C**2 - 2 * B * C * math.cos(alpha_rad)
    return math.sqrt(A_squared)

# Read inputs from user
B = float(input("Enter side B: "))
C = float(input("Enter side C: "))
alpha = float(input("Enter angle between B and C (in degrees): "))

A = third_side(B, C, alpha)
print(f"Length of third side A is: {A:.2f}")

Sample Output (input: B = 5, C = 7, α = 60):


Length of third side A is: 6.24

Written & researched by Dr. Shahin Siami