Python program to determine the roots of a quadratic equation

Here is the code for a python program that determines whether the roots of a quadratic equation are real or imaginary. If the roots are real, the program will print the value of the roots.


# Python program to determine the roots of a quadratic equation

a = int(input('Enter the coefficient of x2: '))
b = int(input('Enter the coefficient of x: '))
c = int(input('Enter the value of constant: '))
disc = b ** 2 - (4 * a * c)
roots1 = (-b + ( b ** 2 - (4 * a * c) )**(1/2)) / (2*a)
roots2 = (-b - ( b ** 2 - (4 * a * c) )**(1/2)) / (2*a)
if disc >= 0:
    print('Roots are real.')
    print(roots1)
    print(roots2)
else:
    print('Roots are imaginary.')

# This code is contributed by Ahmad Shafiq

callout-flag

Love this read? Subscribe to Modernage!

Do you have any queries? You can ask in the comments. We, at Modernage answer each and every question, asked in the comments, so If you have any queries, or you want to know about any specific platform you can ask in the comments below

Author

Leave a Reply

girl-carrying-document

More Articles.

girl-carrying-document

More Articles.