Home » Python » Python programs

# Find the root of the quadratic equation in Python

In this tutorial, we will see **how to find the root of the quadratic equation in Python programming?**

Submitted by Bipin Kumar, on October 09, 2019

**Quadratic Equation**

An equation in the form of **Ax^2 +Bx +C** is a **quadratic equation**, where the value of the variables **A**, **B**, and **C** are constant and **x** is an unknown variable which we have to **find through the Python program**. The value of the variable **A** won't be equal to zero for the quadratic equation. If the value of **A** is zero then the equation will be linear.

Here, we are assuming a **quadratic equation x^2-7x+12=0** which roots are **4** and **-3**.

**Algorithm to solve this problem**

- We store the value of variables
**A**,**B**and**C**which is given by the user and we will use the mathematical approach to solve this. -
Here, we find the value of
**((B*B)-4*A*C)**and store in a variable d.- If the value of the variable d is negative then the value of x will be imaginary numbers and print the roots of the equation is imaginary.
- If the value of the variable is positive then x will be real.

- Since the equation is quadratic, so it has two roots which are x1 and x2.
- When we will find the value of roots of the equation from the above, it may be decimal or integer but we want the answer in an integer that's why we will take
**math.floor()**of the value of the variable x.

x1=(-B+((B*B)-4*A*C) **0.5)/2*A x2=(-B-((B*B)-4*A*C) **0.5)/2*A

### Python program to find the root of the quadratic equation

# importing math module import math A,B,C=map(int,input().split()) d=((B**2)-4*A*C) if d>=0: s=(-B+(d)**0.5)/(2*A) p=(-B-(d)**0.5)/(2*A) print(math.floor(s),math.floor(p)) else: print('The roots are imaginary')

**Output**

1 -7 12 4 3

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.