Q:

Find the root of the quadratic equation in Python

belongs to collection: Python basic programs

0

Quadratic Equation

An equation in the form of Ax^2 +Bx +C is a quadratic equation, where the value of the variables AB, 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

    1. We store the value of variables AB and C which is given by the user and we will use the mathematical approach to solve this.
    2. Here, we find the value of ((B*B)-4*A*C) and store in a variable d.
      1. 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.
      2. If the value of the variable is positive then x will be real.
    3. Since the equation is quadratic, so it has two roots which are x1
    4. and 

x2

      .
x1=(-B+((B*B)-4*A*C) **0.5)/2*A
x2=(-B-((B*B)-4*A*C) **0.5)/2*A
  1. 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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to check the given Date is valid or... >>
<< Python program to find the least multiple from giv...