 # Python program to solve quadratic equation

## Python program to solve quadratic equation

Given a quadratic equation the task is solve the equation or find out the roots of the equation. Standard form of quadratic equation is –

ax2 + bx + c
where,
a, b, and c are coefficient and real numbers and also a ≠ 0.
If a is equal to 0 that equation is not valid quadratic equation. Examples:

Input :a = 1, b = 2, c = 1
Output :
Roots are real and same
-1.0

Input :a = 2, b = 2, c = 1
Output :
Roots are complex
-0.5  + i 2.0
-0.5  - i 2.0

Input :a = 1, b = 10, c = -24
Output :
Roots are real and different
2.0
-12.0


Method 1: Using the direct formula

Using the below quadratic formula we can find the root of the quadratic equation. There are following important cases.

If b*b < 4*a*c, then roots are complex
(not real).
For example roots of x2 + x + 1, roots are
-0.5 + i1.73205 and -0.5 - i1.73205

If b*b == 4*a*c, then roots are real
and both roots are same.
For example, roots of x2 - 2x + 1 are 1 and 1

If b*b > 4*a*c, then roots are real
and different.
For example, roots of x2 - 7x - 12 are 3 and 4
 # Python program to find roots of quadratic equation import math      # function for finding roots def equationroots( a, b, c):        # calculating discriminant using formula     dis = b * b - 4 * a * c      sqrt_val = math.sqrt(abs(dis))            # checking condition for discriminant     if dis > 0:          print(" real and different roots ")          print((-b + sqrt_val)/(2 * a))          print((-b - sqrt_val)/(2 * a))            elif dis == 0:          print(" real and same roots")          print(-b / (2 * a))            # when discriminant is less than 0     else:         print("Complex Roots")          print(- b / (2 * a), " + i", sqrt_val)          print(- b / (2 * a), " - i", sqrt_val)    # Driver Program  a = 1 b = 10 c = -24   # If a is 0, then incorrect equation if a == 0:          print("Input correct quadratic equation")    else:     equationroots(a, b, c)

Output:

real and different roots
2.0
-12.0


Method 2: Using the complex math module

First, we have to calculate the discriminant and then find two solution of quadratic equation using cmath module.

 # import complex math module import cmath   a = 1 b = 4 c = 2   # calculating  the discriminant dis = (b**2) - (4 * a*c)   # find two results ans1 = (-b-cmath.sqrt(dis))/(2 * a) ans2 = (-b + cmath.sqrt(dis))/(2 * a)   # printing the results print('The roots are') print(ans1) print(ans2)

Output:

The roots are
(-3.414213562373095+0j)
(-0.5857864376269049+0j)

Last Updated on November 13, 2021 by admin

## Python – Matplotlib.pyplot ticksPython – Matplotlib.pyplot ticks

Python | Matplotlib.pyplot ticks Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by

## Python – The new generation LanguagePython – The new generation Language

Python – The new generation Language Python designed by Guido van Rossum at CWI has become a widely used general-purpose, high-level programming language. Prerequisites: Knowledge of any programming language can be a plus.   Reason for increasing popularity Emphasis on code readability, shorter codes, ease

## Underscore (_) in PythonUnderscore (_) in Python

Underscore (_) in Python Single Underscore   In Interpreter: _ returns the value of last executed expression value in Python Prompt/Interpreter   >>> a = 10 >>> b = 10 >>> _ Traceback (most recent call last): File "<stdin>", line

## Python – Add image on a Tkinter buttonPython – Add image on a Tkinter button

Python | Add image on a Tkinter button Tkinter is a Python module which is used to create GUI (Graphical User Interface) applications with the help of varieties of widgets and functions. Like any other GUI module it also supports images

## Priority Queue in PythonPriority Queue in Python

Priority Queue in Python Priority Queues are abstract data structures where each data/value in the queue has a certain priority. For example, In airlines, baggage with the title “Business” or “First-class” arrives earlier than the rest. Priority Queue is an

## Pandas dataframe.assign() – PythonPandas dataframe.assign() – Python

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Dataframe.assign() method assign new columns to a

## Ways to concatenate two lists in PythonWays to concatenate two lists in Python

Ways to concatenate two lists in Python Let’s see how to concatenate two lists using different methods in Python. This operation is useful when we have numbers of lists of elements which needs to be processed in a similar manner.

## Python OOPs ConceptsPython OOPs Concepts

Python OOPs Concepts In Python, object-oriented Programming (OOPs) is a programming paradigm that uses objects and classes in programming. It aims to implement real-world entities like inheritance, polymorphisms, encapsulation, etc. in the programming. The main concept of OOPs is to