TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Quadratic Equation

Python Quadratic Equation for beginners and professionals with programs on basics, controls, loops, functions, native data types etc.

<< Back to PYTHON

Python program to solve quadratic equation

Quadratic equation:

Quadratic equation is made from a Latin term "quadrates" which means square. It is a special type of equation having the form of:

ax2+bx+c=0

Here, "x" is unknown which you have to find and "a", "b", "c" specifies the numbers such that "a" is not equal to 0. If a = 0 then the equation becomes liner not quadratic anymore.

In the equation, a, b and c are called coefficients.

Let's take an example to solve the quadratic equation 8x2 + 16x + 8 = 0

See this example:

# import complex math module
import cmath
a = float(input('Enter a: '))
b = float(input('Enter b: '))
c = float(input('Enter c: '))

# calculate the discriminant
d = (b**2) - (4*a*c)

# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2)) 

Output:

Enter a: 8
Enter b: 5
Enter c: 9
The solution are (-0.3125-1.0135796712641785j) and (-0.3125+1.0135796712641785j)

Explanation -

In the first line, we have imported the cmath module and we have defined three variables named a, b, and c which takes input from the user. Then, we calculated the discriminant using the formula. Using the cmath.sqrt() method, we have calculated two solutions and printed the result.

Second Method

We can get the solution of the quadric equation by using direct formula. Let's understand the following example.

Above formula consists of the following cases.

  • If b2 < 4ac, then the roots are complex (not real). For example - x2 + x + 1, roots are -0.5 + i1.73205 and +0.5 - i1.73205.
  • If b2 == 4ac, then the both roots are same For example - x2 + x + 1, roots are -0.5 + i1.73205 and +0.5 - i1.73205.
  • If b2 > 4ac, then the roots are real and different. For example - x2 - 7 x - 12, roots are 3 and 4.

Example -

# Python program to find roots of quadratic equation
import math


# function for finding roots
def findRoots(a, b, c):

    dis_form = b * b - 4 * a * c
    sqrt_val = math.sqrt(abs(dis_form))


    if dis_form > 0:
        print(" real and different roots ")
        print((-b + sqrt_val) / (2 * a))
        print((-b - sqrt_val) / (2 * a))

    elif dis_form == 0:
        print(" real and same roots")
        print(-b / (2 * a))


    else:
        print("Complex Roots")
        print(- b / (2 * a), " + i", sqrt_val)
        print(- b / (2 * a), " - i", sqrt_val)


a = int(input('Enter a:'))
b = int(input('Enter b:'))
c = int(input('Enter c:'))

# If a is 0, then incorrect equation
if a == 0:
    print("Input correct quadratic equation")

else:
    findRoots(a, b, c)

Output:

Enter a:7
Enter b:5
Enter c:2
Complex Roots
-0.35714285714285715  + i 5.5677643628300215
-0.35714285714285715  - i 5.5677643628300215

Explanation -

In the above code, we have imported the math module and defined the formula to calculate discriminant. We have then defined findRoots function which take three integer values as arguments. Then we have checked the roots using the if-elif-else statement.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf