The roots (solutions) to the quadratic equation ax2 +bx+c = 0 is given by the well-known formula x = 2a −b ± √b −4ac 2
Write a python program that takes the three coefficients a, b, and c as input, computes and displays the solutions according to the aforementioned formula. Note the ± in the numerator means there are two answers. But this will not apply if a is zero, so that condition must be checked separately; design your program such that: 1. If a = 0 and b = 0 and c = 0, it prints “solution is indeterminate!”. 2. If a = 0 and b = 0 but c =/ 0 ,it prints “there is no solution”. 3. If a = 0 but b =/ 0 and c =/ 0 , it prints “equation is linear”, computes the solution as x = −c/b and display it. The above formula also fails to work (for real numbers) if the expression under the square root is negative. That expression b ac is called the discriminant of the 2 − 4 quadratic. Define that as a separate variable d and check its sign: 1. If d > 0 then there are two real roots given by the formula above. 2. If d = 0 then the roots are loaded and equal to −b/2a. 3. If d < 0 then there is no real solution (roots are complex numbers).
Consider the following inputs/outputs for your test cases. Input Output a b c 1 -1 -2 There are two real roots. x = 2 and -1 0 -1 -2 The equation is linear. x = -2 0 0 -2 There is no solution. 0 0 0 Solution is indeterminate! 2 4 2 Roots are loaded. x = -1 5 2 1 there is no real solution.
Code (Intedentation is important So I am giving screenshot also) |
a=int(input("Enter a: ")) b=int(input("Enter b: ")) c=int(input("Enter c: ")) if(a==0): if(b==0 and c==0):#1st condition print("Solution Indeterminate") elif(b==0 and c!=0):#2nd condition print("No Solution") elif(b!=0 and c!=0):# 3rd condition print("Equation is linear") x=(-c/b) print("x = ",x)#Printing root for linear else: d=((b*b)-(4*a*c)) # finding condition of roots if(d<0):#Complex roots print("No real solution") elif(d==0):#Equal roots print("Roots are loaded") x=(-b/(2*a)) print("x = ",x) else:#Real roots print("Real Roots") x1=(-b+(d)**0.5)/(2*a) x2= (-b-(d)**0.5)/(2*a) print("x1 = ",x1)#printing root1 print("x2 = ",x2)#printing root2 |
Output:-
Get Answers For Free
Most questions answered within 1 hours.