Saturday, February 9, 2019

Quadratic Equation

This program calculates the roots of a quadratic equation from the co-efficient a, b and c. This program also calculates the roots if they are imaginary.

Code:

import math
print("Enter the co-efficient of quadratic equation.")
a = float(input("Enter a:"))
b = float(input("Enter b:"))
c = float(input("Enter c:"))
d = b**2 - 4*a*c
if d < 0:
real = -b/(2*a)
imag = math.sqrt(-d)/(2*a)
root1 = complex(real,imag)
root2 = complex(real,-imag)
print('The roots are complex:')
print(root1)
print(root2)
elif d == 0:
root = -b/(2*a)
print('The roots are equal:')
print(root)
else:
root1 = (-b + math.sqrt(d))/(2*a)
root2 = (-b - math.sqrt(d))/(2*a)
print('The roots are real:')
print(root1)
print(root2)
view raw Quadratic.py hosted with ❤ by GitHub

No comments: