Saturday, September 4, 2021

Python Program to find the roots of Quadric Equation

Python Program to find the roots of Quadric Equation

The standard form of a quadratic equation is
ax2 + bx + c = 0.
where a, b are the coefficients, x is the variable, and c is the constant.
The Formula to find the roots of a quadratic equation is.

Quadratic Formula = [-b ± √(b² - 4ac)]/2a

If the √(b² - 4ac) = 0 then, we get only one answer = -b/2a
If the √(b² - 4ac) > 0 then, we get two answers = [-b + √(b² - 4ac)]/2a and [-b - √(b² - 4ac)]/2a
If the √(b² - 4ac) < 0 then, we get no answer

Write a Python Program to input a,b,c and print the roots.

Answer

 
import math
print('Finding the roots of the Quadric Equations')
a=int(input('Enter coefficient of x square a?'))
b=int(input('Enter coefficient of x  b?'))
c=int(input('Enter constant c?'))
d=b**2 - (4*a*c)
if d<0:
    print('No Answers')
else:
    if d==0:
        print('Only One Answer', (-1*b)/2*a)
    else:
        print('There are two answers')
        dd=math.sqrt(d)
        print('One Answer is ', float((-1*b + dd)/2*a))
        print('Another Answer is ', float((-1*b - dd)/2*a))
 

Sample Output

 
Finding the roots of the Quadric Equations
Enter coefficient of x square a?1
Enter coefficient of x  b?-4
Enter constant c?4
Only One Answer 2.0


Finding the roots of the Quadric Equations
Enter coefficient of x square a?1
Enter coefficient of x  b?-5
Enter constant c?6
There are two answers
One Answer is  3.0
Another Answer is  2.0


Finding the roots of the Quadric Equations
Enter coefficient of x square a?5
Enter coefficient of x  b?1
Enter constant c?3
No Answers



No comments:

Post a Comment