how would I code this type of problem in python? Section 4.2 *4.I (Algebra: solv
ID: 3754534 • Letter: H
Question
how would I code this type of problem in python?
Section 4.2 *4.I (Algebra: solve quadratic equations) The two roots of a quadratic equation, for example, ar2 bx c 0. can be obtained using the following formula: b2 4ac 2a and r2 2a b2 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots. Write a program that prompts the user to enter valtues for a, j, and c and displays the result based on the discriminant. If the discriminant is positive, display two roots. If the discriminant is 0, display one root. Otherwise, display The equa- tion has no real roots. Here are some sample runs.Explanation / Answer
# This program is written in python 3
import math # Import math library used for math computation
def main():
a = float(input("Enter Value of a: "))
b = float(input("Enter Value of b: "))
c = float(input("Enter Value of c: "))
discriminant = b*b-4*a*c # Compute discriminant
if(discriminant > 0):
r1 = (-b + math.sqrt(discriminant))/(2*a)
r2 = (-b - math.sqrt(discriminant))/(2*a)
print("root 1 is = ", r1)
print("root 2 is = ", r2)
elif(discriminant == 0):
r1 = -b/(2*a)
print("Both roots are equal and root is = ", r1)
else:
print("The equation has no real roots")
main()
Output:
Enter Value of a: 2
Enter Value of b: 8
Enter Value of c: 3
root 1 is = -0.41886116991581024
root 2 is = -3.58113883008419
Output:
Enter Value of a: 2
Enter Value of b: 4
Enter Value of c: 2
Both roots are equal and root is = -1.0
ouput:
Enter Value of a: 4
Enter Value of b: 5
Enter Value of c: 4
The equation has no real roots
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.