Wap in python to find roots of a quadratic equation and its type(real, and eqaul , real and unequal)
Answers
Heya friend,
Here we go :
Before we move on to the code,
First let us understand about roots of the quadratic equation.
roots can be founded by discriminant method which we'll be using here :-
-b± / 2a
where,
d =
and,
Now, Let's move on to the type of the equation :
if d=0 The roots are real and equal
if d > 0 The roots are real and unequal
if d<0 No real roots of the equation exist
NOTE :-
- We know, if a = 0, then the we do not consider the equation as quadratic, so we will use this concept in the program also.
- Here, we have to use the square root function which is a module function, for that we have to call that by calling the math module.
Now' let's see the code:
SOURCE CODE
#To find roots of a quadratic equation and it's type
a = float(input('coefficient of x^2 = '))
b = float(input('coeeficient of x = '))
c = float(input('Enter the constant term = '))
import math
if a==0:
print('not a quadratic equation')
else:
d=b**2-(4*a*c)
if d==0:
x1 = x2 = -b/(2*a)
print('The roots are real and equal')
print('The roots are :',x1,'and',x2)
elif d>0:
x1 = -b + math.sqrt(d)/(2*a)
x2 = -b + math.sqrt(d)/(2*a)
print('The roots are real and unequal')
print('The roots are :',x1,'and',x2)
else:
print('No real roots of the quadratic equation exist')
=================================================
Now, for the output, I'll show the output for all the 4 cases when the roots are real and equal, real and unequal, no real roots and not a quadratic equation
=================================================
OUTPUT
coefficient of x^2 = 1
coeeficient of x = -4
Enter the constant term = 4
The roots are real and equal
The roots are : 2.0 and 2.0
>>>
>>>
coefficient of x^2 = 0
coeeficient of x = 1
Enter the constant term = 1
not a quadratic equation
>>>
>>>
coefficient of x^2 = 1
coeeficient of x = 1
Enter the constant term = -2
The roots are real and unequal
The roots are : 0.5 and 0.5
>>>
>>>
coefficient of x^2 = 1
coeeficient of x = 2
Enter the constant term = 3
No real roots of the quadratic equation exist
>>>
Thanks !
#BAL #answerwithquality
@MANAV
Answer:-
import math
a = float(input('Enter value of a = '))
b = float(input('Enter value of b = '))
c = float(input('Enter value of c = '))
if a==0:
print('This is not a quadratic equation.')
else:
d=b* *2-(4*a*c)
if d==0:
x1 = x2 = -b/(2*a)
print('The roots are real and equal.')
print('The roots are :',x1,'and',x2)
elif d>0:
x1 = -b + math.sqrt(d)/(2*a)
x2 = -b + math.sqrt(d)/(2*a)
print('The roots are real and unequal.')
print('The roots are :',x1,'and',x2)
else:
print('The Roots are imaginary and unequal.')
✨