-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab2.py
More file actions
45 lines (32 loc) · 1.35 KB
/
Lab2.py
File metadata and controls
45 lines (32 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -------------------------------------- Task 1 -----------------------------------
def add(x, y):
return x + y
# TODO: Add definitions of sub(), div(), mult(), exp(), as well as neg() and sqrt().
# neg() should return the negation of the given number, and sqrt() should
# return the square root of the given number.
def sub(x, y):
return x - y
def mult(x, y):
return x * y
def div(x, y):
return x / y
def exp(x, y):
return x ** y
def neg(x):
return x * (-1)
def sqrt(x):
return x ** 0.5
# -------------------------------------- Task 2 -----------------------------------
# TODO: Implement the quadratic formula using *only* the functions defined here.
# Do not use arithmetic operators directly.
# You're allowed to define other functions.
print("Let's find the roots of a quadratic function 'ax^2 + bx + c'") # instructions
print()
a = float(input("What is the value of a? a = ")) # input values
b = float(input("What is the value of b? b = "))
c = float(input("What is the value of c? c = "))
x1 = div(add(neg(b), sqrt(sub(exp(b, 2), mult(4, mult(a, c))))) , mult(2,a)) # first root of the quadratic equation
x2 = div(sub(neg(b), sqrt(sub(exp(b, 2), mult(4, mult(a, c))))) , mult(2,a)) # second root of the quadratic equation
print()
print("First root: " + str(x1)) # show results
print("Second root: " + str(x2))