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:
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
No comments:
Post a Comment