Quadratic Equation

    0

    4

    Mahendra Kumar

    mathslogicsolvingEquations

    Solving a given quadratic equation. The nature of the roots are given as, If discriminant>1 then the roots are real and different If discriminant=0 then the roots are real and equal If discriminant<1 then the roots are complex and different For the quadratic equation axΒ² + bx + c = 0, if we denote the discriminant as d, then their roots

    If d>1 then the roots are real and different root1 = (-b + √d)/2a root2 = (-b – √d)/2a If d=0 then both roots are -b/2a

    If d<1 then roots are complex and different root1 = -b/2a + i (√d/2a) root2 = -b/2a – i (√d/2a)

    import math  #importing math-module
    # take inputs
    a = int(input('Enter the value of a: '))
    b = int(input('Enter the value of b: '))
    c = int(input('Enter the value of c: '))
    # calculate discriminant
    dis = (b**2) - (4*a*c)
    # checking condition for discriminant
    if(dis > 0):
        root1 = (-b + math.sqrt(dis) / (2 * a))
        root2 = (-b - math.sqrt(dis) / (2 * a))
        print("Two distinct real roots are %.2f and %.2f" %(root1, root2))
    elif(dis == 0):
        root1 = root2 = -b / (2 * a)
        print("Two equal and real roots are %.2f and %.2f" %(root1, root2))
    elif(dis < 0):
        root1 = root2 = -b / (2 * a)
        imaginary = math.sqrt(-dis) / (2 * a)
        print("Two distinct complex roots are %.2f+%.2f and %.2f-%.2f" 
                              %(root1, imaginary, root2, imaginary))
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.