Computer Science, asked by neerajkapoor79936, 4 months ago

WAP to calculate roots of quadratic equation ax2 + bx + c=0​

Answers

Answered by Anonymous
2

The best way to write this program is to directly use the quadratic formula

 \frac{-b \pm \sqrt{b^2-4ac}}{2a}

#Program by Ankitraj707

import math

# function for finding roots

def roots( a, b, c):

# calculating discriminant using formula

dis = b * b - 4 * a * c

sqrt_val = math.sqrt(abs(dis))

# checking condition for discriminant

if dis > 0:

 \: \: \: \: print(" real and different roots ")

 \: \: \: \: print((-b + sqrt_val)/(2 * a))

 \: \: \: \: print((-b - sqrt_val)/(2 * a))

elif dis == 0:

 \: \: \: \: print(" real and same roots")

 \: \: \: \: print(-b / (2 * a))

# when discriminant is less than 0

else:

 \: \: \: \: print("Complex Roots")

 \: \: \: \: print(- b / (2 * a), " + i", sqrt_val)

 \: \: \: \: print(- b / (2 * a), " - i", sqrt_val)

# Driver Program

a = 1

b = 10

c = -24

# If a is 0, then incorrect equation

if a == 0:

 \: \: \: \: print("Input correct quadratic equation")

else:

 \: \: \: \: roots(a, b, c)

#Keep asking such questions

Similar questions