1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
# solve a quadratic equation
from math import sqrt
# here is for the user to give values to a,b and c
a = int(input('enter the first coefficient : a = '))
b = int(input('enter the second coefficient : b = '))
c = int(input('enter the third coefficient : c = '))
# the form of quadratic equations is ax^2 + bx + c
print('the equation you want to solve is : ', a,'x² + ',b,'x + ',c,' = 0')
# well now we have to case the first one is when a = 0 and the second is a not equal 0
# let's begging whith the first one a = 0
if a == 0 :
# so here the equation is writting like this
print('the new form of the equation is ',b,'*x +', c,' = 0')
# the solution is
x = (-c)/b
print('the solution is : x = ',x)
else :
# this is discriminant of the equation
delta = b**2 - (4*a*c)
# we have here three cas
# the first one is:
if delta > 0:
x1 = (-b + sqrt(delta))/(2*a)
x2 = (-b - sqrt(delta))/(2*a)
print("the two solution of the equation are : x = ",x1,' or x = ', x2)
# the second cas :
elif delta < 0 :
print('there is no solutions of this equation in the real numbers set')
# the last cas :
else :
x0 = -b/(2*a)
print('there is only one solution of this equation, it is : x = ',x0)
# the end of the programme |
Partager