How can we find roots of a Quadratic equation in Python? We can solve any quadratic equation by completing the square, rewriting part of the equation as a perfect square trinomial. If we complete the square on the generic equation ax2 + bx + c = 0 and then solve for x, then we will get the root.

#### Quadratic equation in Python :

```
import math
a = int(input("Please Enter a Value of a Quadratic Equation : "))
b = int(input("Please Enter b Value of a Quadratic Equation : "))
c = int(input("Please Enter c Value of a Quadratic Equation : "))
discriminant = (b * b) - (4 * a * c)
if(discriminant > 0):
root1 = (-b + math.sqrt(discriminant) / (2 * a))
root2 = (-b - math.sqrt(discriminant) / (2 * a))
print("Two Distinct Real Roots Exists: root1 = %.2f and root2 = %.2f" %(root1, root2))
elif(discriminant == 0):
root1 = root2 = -b / (2 * a)
print("Two Equal and Real Roots Exists: root1 = %.2f and root2 = %.2f" %(root1, root2))
elif(discriminant < 0):
root1 = root2 = -b / (2 * a)
imaginary = math.sqrt(-discriminant) / (2 * a)
print("Two Distinct Complex Roots Exists: root1 = %.2f+%.2f and root2 = %.2f-%.2f" %(root1, imaginary, root2, imaginary))
```

## 0 comments:

## Post a Comment

Share your post related problems with us.