Python Assignment - Roots & Reciprocals

This week I completed an assignment that covered:

Solving a quadratic equation using the quadratic formula
Printing reciprocals using a for loop


Problem 1: Roots of a Quadratic

Equation: x² - 5.86x + 8.5408 = 0


Code:

import math


a = 1

b = -5.86

c = 8.5408


discriminant = b**2 - 4*a*c

root1 = (-b + math.sqrt(discriminant)) / (2*a)

root2 = (-b - math.sqrt(discriminant)) / (2*a)


print("Root 1:", root1)

print("Root 2:", root2)


Output:

Root 1: 3.1400000000000006

Root 2: 2.7199999999999998


Problem 2: Reciprocals (1/2 to 1/10)


Code:

for i in range(2, 11):

    print(f"1/{i} = {1/i}")


Output:

1/2 = 0.5

1/3 = 0.3333333333333333

1/4 = 0.25

1/5 = 0.2

1/6 = 0.16666666666666666

1/7 = 0.14285714285714285

1/8 = 0.125

1/9 = 0.1111111111111111

1/10 = 0.1

Comments

Popular posts from this blog

Python Assignment – Intro to Functions and Lists

Using a Rectangle Class in Python