0

Write a python code in finding the roots of a quadratic equation?

Can someone help with this...I am not getting the desired output. Also apart from string commands, is there any other way to remove the brackets from the output? import cmath a=int(input()) b=int(input()) c=int(input()) d = (b**2) - (4*a*c) x = (-b-cmath.sqrt(d))/(2*a) y = (-b+cmath.sqrt(d))/(2*a) r1=str(x) q1=r1[1:6] r2=str(y) q2=r2[1:6] print("output is %s and %s" %(q1, q2))

1st Oct 2021, 9:48 AM
Saurav Singh
Saurav Singh - avatar
1 Odpowiedź
0
hey what exactly do you need why are you slicing ? cmath.sqrt returns a complex number so it is in brackets .. if you want real & imaginary parts try this x.real ,y.imag props of complex numbers And I do this way a=int(input()) b=int(input()) c=int(input()) dRoot = ((b**2) - (4*a*c))**(1/2) x = (-b-dRoot)/(2*a) y = (-b+dRoot)/(2*a) print('Roots of {}x**2+({})x+({}) are {},{}'.format(a,b,c,round(x,4),round(y,4)))
1st Oct 2021, 10:44 AM
Gireesh Desineni
Gireesh Desineni - avatar