Find the root of the quadratic equation in Python

Quadratic Equation

An equation as Ax^2 +Bx +C is a quadratic equation, where the estimation of the factors A, B, and C are steady and x is an obscure variable which we need to discover through the Python program. The estimation of variable A won’t be equivalent to zero for the quadratic equation. On the off chance that the estimation of An is zero, at that point the equation will be straight.

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

The calculation to take care of this issue

We store the estimation of factors A, B and C which is given by the client and we will utilize the scientific way to deal with fathom this.

Here, we discover the estimation of ((BB)- 4A*C) and store in a variable d.

In the event that the estimation of the variable d is negative, at that point the estimation of x will be fanciful numbers and print the foundations of the equation is nonexistent.

On the off chance that the estimation of the variable is certain, at that point x will be genuine.

Since the equation is quadratic, so it has two roots which are x1

furthermore, x2.

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

At the point when we will discover the estimation of foundations of the equation from the abovementioned, it might be decimal or number however we need the appropriate response in a whole number that is the reason we will take math.floor() of the estimation of the variable x.

Python program to discover the base 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

Leave a Comment

error: Alert: Content is protected!!