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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
# solve a quadratic equation in the real numbers set
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 tow cases 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')
# here if b = 0
if b == 0:
# here if c = 0
if c == 0 :
print('there are infinity of solutions')
# here if c is not equal 0
else :
print('there are no solutions')
# here if b is not equal 0
else :
if c == 0 :
print('x = 0')
# here if c is not equal 0
else :
x = (-c)/b
print('the solution is : x = ',x)
# here if is a isn't equal 0
else :
# if b = 0
if b == 0 :
# if c = 0
if c == 0:
print('x = 0')
# if c isn't equal 0
else :
y = -c/a
# here if y is positive
if y > 0 :
print('x = ', sqrt(-c/a), 'ou x = ', -sqrt(-c/a) )
# here if y is not positive so the sqrt of y doesn't exist in the real numbers set
else :
print('there is no solutions')
# if b isn't equal 0
else :
if c == 0 :
print('x = 0 ou x = ',-b/a )
#here if c isn't equal 0
else :
# this is discriminant of the equation
delta = b**2 - (4*a*c)
# the first case :
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 case :
elif delta < 0 :
print('there is no solutions of this equation in the real numbers set')
# the last case :
else :
x0 = -b/(2*a)
print('there is only one solution of this equation, it is : x = ',x0)
# the end of the programme |
Partager