How to Check the Nature of Roots of a Quadratic Equation in Python?

Quadratic equation:

Quadratic equation is derived from the Latin term “quadrates,” which means “square.” The equation of the Quadratic equation is:

ax2+bx+c=0

Here, “x” is an unknown that you must find, and “a”, “b”, and “c” are numbers such that “a” is not equal to 0. If a = 0, the equation becomes linear rather than quadratic.

The letters a, b, and c in the equation are known as coefficients.

where c= constant

Discrimination is defined as follows:

discriminant = (b^2 – 4ac)

To determine the nature of the quadratic equation’s roots, we must first determine the value of its discriminant. For example, if we get a discriminant value more than 0 or can say positive, then the roots are “Distinct & Real.” The following are the different conditions of the discriminant and their respective values:

  • Real and Distinct roots: if discriminant >0
  • Roots are Equal:  if discriminant =0
  • Roots are Imaginary: if discriminant <0

Check the Nature of Roots of a Quadratic Equation in Python

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the a value as static input and store it in a variable.
  • Give the b value as static input and store it in another variable.
  • Give the c value as static input and store it in another variable.
  • Calculate the discriminant value of a quadratic equation using the above given mathematical formula and store it in a variable.
  • Print the discriminant value of a quadratic equation
  • Check if the above discriminant value is greater than 0 using the if conditional statement
  • If it is true, then print the roots of a quadratic equation are Real
  • Check if the above discriminant value is equal to 0 using the if conditional statement
  • If it is true, then print the roots of a quadratic equation are Equal
  • Check if the above discriminant value is less than 0 using the if conditional statement
  • If it is true, then print the roots of a quadratic equation are Imaginary.
  • The Exit of the Program.

Below is the implementation:

# Give the a value as static input and store it in a variable.
gvn_a = 4
# Give the b value as static input and store it in another variable.
gvn_b = 5
# Give the c value as static input and store it in another variable.
gvn_c = 6
# Calculate the disciminant value of a quadratic equation using the above
# given mathematical formula  and store it in a variable.
disciminant = gvn_b**2 -(4* gvn_a *gvn_c)  
# print the disciminant value of a quadratic equation
print("The disciminant value of a quadratic equation = ", disciminant)

# Check if the above discriminat value is greater than 0 using the if conditional statement
if disciminant>0: 
  # If it is true, then print the roots of a quadratic equation are Real
  print("The roots of a quadratic equation are Real")
# Check if the above discriminant value is equal to 0 using the if conditional statement
if disciminant==0: 
  # If it is true, then print the roots of a quadratic equation are Equal
  print("The roots of a quadratic equation are Equal")
# Check if the above discriminant value is less than 0 using the if conditional statement
if disciminant<0: 
  # If it is true, then print the roots of a quadratic equation are Imaginary
  print("The roots of a quadratic equation are Imaginary")

Output:

The disciminant value of a quadratic equation = -71
The roots of a quadratic equation are Imaginary

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the a value as user input using the int(input()) function and store it in a variable.
  • Give the b value as user input using the int(input()) function and store it in another variable.
  • Give the c value as user input using the int(input()) function and store it in another variable.
  • Calculate the discriminant value of a quadratic equation using the above given mathematical formula and store it in a variable.
  • Print the discriminant value of a quadratic equation
  • Check if the above discriminant value is greater than 0 using the if conditional statement
  • If it is true, then print the roots of a quadratic equation are Real
  • Check if the above discriminant value is equal to 0 using the if conditional statement
  • If it is true, then print the roots of a quadratic equation are Equal
  • Check if the above discriminant value is less than 0 using the if conditional statement
  • If it is true, then print the roots of a quadratic equation are Imaginary.
  • The Exit of the Program.

Below is the implementation:

# Give the a value as user input using the int(input()) function
# and store it in a variable.
gvn_a = int(input("Enter some random 'a' value = "))
# Give the b value as user input using the int(input()) function
# and store it in another variable.
gvn_b = int(input("Enter some random 'b' value = "))
# Give the c value as user input using the int(input()) function
# and store it in another variable.
gvn_c = int(input("Enter some random 'c' value = "))
# Calculate the disciminant value of a quadratic equation using the above
# given mathematical formula  and store it in a variable.
disciminant = gvn_b**2 -(4* gvn_a *gvn_c)  
# print the disciminant value of a quadratic equation
print("The disciminant value of a quadratic equation = ", disciminant)

# Check if the above discriminat value is greater than 0 using the if conditional statement
if disciminant>0: 
  # If it is true, then print the roots of a quadratic equation are Real
  print("The roots of a quadratic equation are Real")
# Check if the above discriminant value is equal to 0 using the if conditional statement
if disciminant==0: 
  # If it is true, then print the roots of a quadratic equation are Equal
  print("The roots of a quadratic equation are Equal")
# Check if the above discriminant value is less than 0 using the if conditional statement
if disciminant<0: 
  # If it is true, then print the roots of a quadratic equation are Imaginary
  print("The roots of a quadratic equation are Imaginary")

Output:

Enter some random 'a' value = 1
Enter some random 'b' value = -5
Enter some random 'c' value = 6
The disciminant value of a quadratic equation = 1
The roots of a quadratic equation are Real