Julia V.
Verzosa
CS15L (6542)
Let's Analyze: Python Coding for Mathematical Problems
# Let’s Analyze - Math Functions
import math
# 1. Convert degrees to radians
print("1. Convert degrees to radians")
degree = float(input("Enter angle in degrees: "))
radian = math.radians(degree)
print(f"{degree} degrees in radians is: {radian:.4f}")
print("-" * 50)
# 2. Calculate the quadratic equation
print("2. Calculate the quadratic equation (ax^2 + bx + c = 0)")
# Coefficients
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
# Discriminant
discriminant = b**2 - 4*a*c
if discriminant > 0:
root1 = (-b + math.sqrt(discriminant)) / (2 * a)
root2 = (-b - math.sqrt(discriminant)) / (2 * a)
print(f"Roots are real and different: {root1:.4f}, {root2:.4f}")
elif discriminant == 0:
root = -b / (2 * a)
print(f"Roots are real and same: {root:.4f}")
else:
real_part = -b / (2 * a)
imaginary_part = math.sqrt(abs(discriminant)) / (2 * a)
print(f"Complex Roots: {real_part:.4f} ± {imaginary_part:.4f}i")
print("-" * 50)
# 3. Calculate Sine, Cosine, and Tangent (SOH-CAH-TOA)
print("3. Calculate SOH-CAH-TOA")
# Assume the variables:
adj = 10
opp = 5
hyp = 20
# Calculate trigonometric functions
sine = opp / hyp
cosine = adj / hyp
tangent = opp / adj
print(f"Sine (SOH) = {sine:.4f}")
print(f"Cosine (CAH) = {cosine:.4f}")
print(f"Tangent (TOA) = {tangent:.4f}")
print("-" * 50)
Output