Python program to find the roots of a quadratic equation

Python program to find the roots of a quadratic equation

In this article, We will be going to see How to find the roots of a quadratic equation: A*x^2 + B*x + C.

Example:

Input: Quadratic Equation: 1X^2 + 5X + 2
Output: The Roots of a Quadratic Equations are:  -0.4384 and -4.5616

for finding the roots we are using given below rules:

D = B^2 - 4*A*C

If D is greater than or equal to zero then real roots is exist and then it is calculated using :
root1 = -B + sqrt(D) / 2 * A and root2 =  -B - sqrt(D) / 2 * A.
If D is less than zero then the real roots not exist.

Now, let’s see the different ways to code this program:
Code 1: In this code, we are going to take inputs from user then type cast it into integer and then applying rules for finding roots.

# taking inputs from user and
# convert it into integer
var_a = int(input("Enter the A coefficient:"))
var_b = int(input("Enter the B coefficient:"))
var_c = int(input("Enter the C coefficient:"))

# show the quadratic equation
print("Quadratic Equation: "+ str(var_a)+ "X^2",
      "+", str(var_b) + "X", "+", var_c)

# calculate the value of D
# D = b^2 - 4ac
var_d = var_b * var_b - 4 * var_a * var_c

# check for real roots
# exists condition
if var_d >= 0 :

    # finding the roots and round of the
    # calculated values upto 4 places
    root1 = round(( -var_b + var_d ** (1/2) ) / 2 * var_a, 4)
    root2 = round(( -var_b - var_d ** (1/2) ) / 2 * var_a, 4)

    print("The Roots of a Quadratic Equations are: ",
          root1, "and", root2)

else :
    print("Imaginary Roots")

Output:

Enter the A coefficient:1
Enter the B coefficient:5
Enter the C coefficient:2
Quadratic Equation: 1X^2 + 5X + 2
The Roots of a Quadratic Equations are:  -0.4384 and -4.5616

Code 2:

# main code
if __name__ == "__main__" :
    
    # inputs given
    var_a = 1
    var_b = 5
    var_c = 2

    # show the quadratic equation
    print("Quadratic Equation: "+ str(var_a)+ "X^2",
          "+", str(var_b) + "X", "+", var_c)

    # calculate the value of D
    # D = b^2 - 4ac
    var_d = var_b * var_b - 4 * var_a * var_c

    # check for real roots
    # exists condition
    if var_d >= 0 :

        # finding the roots and round of the
        # calculated values upto 4 places
        root1 = round(( -var_b + var_d ** (1/2) ) / 2 * var_a, 4)
        root2 = round(( -var_b - var_d ** (1/2) ) / 2 * var_a, 4)

        print("The Roots of a Quadratic Equations are: ",
              root1, "and", root2)

    else :
        print("Imaginary Roots")

Output:

Quadratic Equation: 1X^2 + 5X + 2
The Roots of a Quadratic Equations are:  -0.4384 and -4.5616

Code 3: By creating a user defined function.

# Create a user defined function for
# finding roots of a quadratic equation
def findRoots(var_a, var_b, var_c) :

    # calculate the value of D
    # D = b^2 - 4ac
    var_d = var_b * var_b - 4 * var_a * var_c

    # check for real roots
    # exist condition
    if var_d >= 0 :

        # finding the roots and round of the
        # calculated values upto 4 places
        root1 = round(( -var_b + var_d ** (1/2) ) / 2 * var_a, 4)
        root2 = round(( -var_b - var_d ** (1/2) ) / 2 * var_a, 4)

        print("The Roots of a Quadratic Equations are: ",
              root1, "and", root2)

    else :
        print("Imaginary Roots")

# main code
if __name__ == "__main__" :
    
    # inputs given
    var_a = 1
    var_b = 5
    var_c = 2

    # show the quadratic equation
    print("Quadratic Equation: "+ str(var_a)+ "X^2",
          "+", str(var_b) + "X", "+", var_c)

    # function calling
    findRoots(var_a, var_b, var_c)

Output:

Quadratic Equation: 1X^2 + 5X + 2
The Roots of a Quadratic Equations are:  -0.4384 and -4.5616

Code 4: By creating a user defined function along with lambda function.

# Create a anonymous/lambda function for
# calculating the value of D
# D = b^2 - 4ac
find_d = lambda var_a, var_b, var_c : var_b * var_b - 4 * var_a * var_c

# Create a anonymous/lambda function for
# finding the roots and round of the
# calculated values upto 4 places
find_root1 = lambda var_a, var_b, var_d : round(( -var_b + var_d ** (1/2) ) / 2 * var_a, 4)

find_root2 = lambda var_a, var_b, var_d : round(( -var_b - var_d ** (1/2) ) / 2 * var_a, 4)

# Create a user defined function for
# finding roots of a quadratic equation
def findRoots(var_a, var_b, var_c) :

    # function calling
    var_d = find_d(var_a, var_b, var_c)

    # check for real roots
    # exist condition
    if var_d >= 0 :

        # function calling
        root1 = find_root1(var_a, var_b, var_d)
        root2 = find_root2(var_a, var_b, var_d)

        print("The Roots of a Quadratic Equations are: ",
              root1, "and", root2)

    else :
        print("Imaginary Roots")
        
# main code
if __name__ == "__main__" :
    
    # inputs given
    var_a = 1
    var_b = 5
    var_c = 2

    # show the quadratic equation
    print("Quadratic Equation: "+ str(var_a)+ "X^2",
          "+", str(var_b) + "X", "+", var_c)

    # function calling
    findRoots(var_a, var_b, var_c)

Output:

Quadratic Equation: 1X^2 + 5X + 2
The Roots of a Quadratic Equations are:  -0.4384 and -4.5616

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds