Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

roots of quadratic equation in python

#how to find roots in quadratic equation
a=int(input('Enter coefficient of x2 :'))
b=int(input('Enter coefficient of x :'))
c=int(input('Enter the constant :'))
if a==0:
  print("a can't be 0")
else:
  D=b**2-4*a*c
  if D>0:
   print('The roots are real and distinct')
   r1=(-b+D**0.5)/(2*a)
   r2=(-b-D**0.5)/(2*a)
   print("The roots are",r1,"and",r2)
  elif D==0:
   print('The roots are real and equal')
   r=-b/(2*a)
   print('The root is',r)
  else:

   print('The roots are imaginary')
#output:
#real and equal
Enter coefficient of x2 :1
Enter coefficient of x :-4
Enter the constant :4
The roots are real and equal
The root is 2.0
#not real
Enter coefficient of x2 :4
Enter coefficient of x :5
Enter the constant :6
The roots are imaginary
#real and not equal
Enter coefficient of x2 :1
Enter coefficient of x :-5
Enter the constant :6
The roots are real and distinct
The roots are 3.0 and 2.0
 
PREVIOUS NEXT
Tagged: #roots #quadratic #equation #python
ADD COMMENT
Topic
Name
8+5 =