31.5k views
5 votes
Input Format:

Get a, b, and c as input.

Output Format:

Output “Yes” or “No” depending on if the given quadratic is factorable. If it is

factorable, output the roots in increasing order. If there are two of the exact same roots, only output it once. Round roots to the nearest hundredth.

Sample Input Sample Output

1 1 -1 Yes -1.00, 0.00

1 3 2 Yes -2.00, -1.00

2 1 2 No

1 Answer

5 votes

In python 3:

import math

def quadratic(a, b, c):

if (b ** 2) - (4 * a * c) < 0:

return "No"

root1 = round((-b + (math.sqrt((b ** 2) - (4 * a * c)))) / (2 * a), 2)

root2 = round((-b - (math.sqrt((b ** 2) - (4 * a * c)))) / (2 * a), 2)

lst = ([])

lst.append(root1)

lst.append(root2)

lst.sort()

return f"Yes {lst[0]}, {lst[1]}"

print(quadratic(1, 3, 2))

The print statement tests the function. I hope this helps!

User NcJie
by
4.3k points