A Simple Program for Solving Quadratic Equations in Python

A Simple Program for Solving Quadratic Equations in Python

A quadratic equation is an equation that is of the form ax^2+bx+c=0. x is the unknown variable whose solution is being sought, while a, b, and c are constants, known as the quadratic coefficient, the linear coefficient, and the constant term respectively; the quadratic coefficient (a) cannot take the value of zero (otherwise the equation becomes a linear equation). The most common way of solving for x is by using the special quadratic formula x= (-b ± √(b^2 )-4ac)/2a, which gives a pair of solutions for x. (b^2 -4ac) is known as the discriminant; it is the key to various possible solutions for the quadratic equation. When the discriminant is positive, x has two simple solutions, with each solution taking a different value; when it is zero, x has two solutions of equal values; and when it is negative, x has two complex solutions, with each solution taking an imaginary number. How can we solve for the unknown variable in quadratic equations using Python? In this short write-up, I present a simple program for solving such tasks, using the special formula mentioned above.

from math import sqrt

# Input the values for a, b, c
a = float(input("a = "))
b = float(input("b = "))
c = float(input("c = "))

# Calculate the discriminant, d

d = (b**2) - (4 * a * c)

# Excluding complex solutions

if d < 0:
  print("This set of values produces complex solutions")
else:

  # Find the square root of the discriminant

  square_root_d = sqrt(d)

  # Find the two solutions
  solution_1 = round((-b - square_root_d)/(2 * a), 2)
  solution_2 = round((-b + square_root_d)/(2 * a), 2)

  # Print the final answer
  print(f"The solutions are {solution_1} and {solution_2}")

When the user runs this program, s/he will be required to input the values for a, b, and c, and then the final pair of solutions for x will be printed out (rounded to two decimal places). The input values are converted to float, so that the user is able to find solutions for the unknown, not only when the constants are integers but also when they are of the float data type. Here, I present two examples. In the first example, the quadratic equation is of the form 2x^2 -5x - 3 = 0. When 2, -5, and -3 are inputted as values for a, b, and c respectively, the program returns the pair of solutions as (-0.5, 3.0):

a = 2
b = -5
c = -3
The solutions are -0.5 and 3.0

In the second case, the quadratic equation is of the form 0.6x^2 +0.5x - 2 = 0. When 0.6, 0.5, and -2 are inputted as values for a, b, and c respectively, the program returns the pair of solutions as (-2.29, 1.46):

a = 0.6
b = 0.5
c = -2
The solutions are -2.29 and 1.46

For the sake of simplicity, this program is limited to all values of a, b, and c for which the discriminant (d) is positive or zero (i.e. d ≥ 0). I have introduced an if-else statement to cater for cases of both d < 0 and d ≥ 0. If the values of a, b, and c are such that d is negative, the sentence "This set of values produce complex solutions" is printed out in the console and no further action takes place. Else, the equation is solved and the results presented. You can try your hands on the program using the following link: https://replit.com/@abdulrahim88/Quadratic-Equations?v=1

Reference

mathsisfun.com/algebra/quadratic-equation.h..

en.wikipedia.org/wiki/Quadratic_equation