Exercise 3:
Les racines d'une équation du second degré en Python
Écrivez un programme en Python qui reçoit les coefficients d'une équation du second degré (une équation quadratique) en entrée et calcule et imprime ses racines.
Code:
a = float(input("a = ")) b = float(input("b = ")) c = float(input("c = ")) delta = (b**2)-(4*a*c) if delta > 0: x1 = (-b + (delta**0.5))/(2*a) x2 = (-b - (delta**0.5))/(2*a) print("x1 = ", x1) print("x2 = ", x2) elif delta == 0: x = -b/(2*a) print("x1 = x2 = ", x) else: print("No Real Roots!")
Exécution:
========= RESTART: C:\Roots of a Quadratic Equation.py ======== a = 1 b = -2 c = -3 x1 = 3.0 x2 = -1.0 >>> ========= RESTART: C:\Roots of a Quadratic Equation.py ======== a = 3 b = -8 c = 5 x1 = 1.6666666666666667 x2 = 1.0 >>> ========= RESTART: C:\Roots of a Quadratic Equation.py ======== a = 1 b = 2 c = 1 x1 = x2 = -1.0 >>> ========= RESTART: C:\Roots of a Quadratic Equation.py ======== a = 3 b = 4 c = 2 No Real Roots! >>>