WAP (python) to find the roots of a given quadratic expression - BoiCoder

BoiCoder

Coding Made Easy for All

Breaking

Tuesday 1 September 2020

WAP (python) to find the roots of a given quadratic expression



Hello everyone today we are going to write a program in python to find the roots of a quadratic equation. So gear up as we are going to write this program in the best way possible. We are going to follow a step wise procedure to write this program.

If you want the written program, you can find it at the end of the article. Otherwise stick to us as we are going to go through every step that you should follow to improve your problem solving skills.

Overview:

A quadratic expression is a polynomial in x in the power of 2. Its general expression looks something like this:
ax2 + bx +c

In this program we are asked to find the roots of a given quadratic equation. The user shall be able to give the initial information ie., the coefficients of the x2, x and the constant terms respectively. Then we would have to calculate the roots of the quadratic equation using the Quadratic Formula (Shri-Dharacharya formula)

* As I have already told you the steps to be performed, you could try to do the program on your own.*


Understanding what we need to do: 


Here I will tell you step wise what you need to do.


Step 1: What to take as input from the user:


As told already, the general expression of a quadratic in x is given by: ax2 + bx +c. Here, we need the user to input a, b and c because they affect the roots of the quadratic expression. 

Here's the code you should write for this step: 

Hence, the first step is to ask the user to input a, b and c.

Step 2: Doing the calculations:


To solve the calculation part, we need to refer to the quadratic formula (or the Shri-Dharacharya formula). As per the quadratic formula, the roots of a quadratic equation can be taken out using the following calculations:

* To find roots of ax2 + bx + c, first find the discriminant i.e., the value of b2-4ac . Let us call the value as d. Now put the value of d in the following equation:
xroot 1= ((-b) + sqrt(d))/ (2*a)     and,
xroot 2= ((-b) - sqrt(d))/ (2*a) 
We would also have to see if the equation has real roots or not. For that we have to see, if value of d<0, then the roots are not real.

Here's the code for this step:

Note that, to find the square root of d, we need to use the 'sqrt' function present in the 'math' library. Hence, we need to import the math library using the import function. So here we have found the roots of the quadratic equation. The full program looks something like this: And here ends our amazing tutorial where we learnt how to find the roots of a given quadratic equation in Python. I hope you liked the program and I believe that you have learnt how to tackle such questions. Please subscribe to our newsletter to get updated with latest tutorials on programming and share if you like it!
Thanks for reading!!!

2 comments:

  1. Thank you for this. I was really struggling at it.

    ReplyDelete
    Replies
    1. Thanks for reading. I hope this helped you. Do checkout our other programs

      Delete