Python programming
Write a program that prompts the user to input the three coefficients a, b, and c of a quadratic equationax2+bx+c= 0.The program should display the solutions of this equation, in the following manner:
1. If the equation has one solution, display ONE SOLUTION:, followed by the solution, displayed with4 digits printed out after the decimal place.
2. If the equation has two real solutions, display TWO REAL SOLUTIONS:, followed by the two solutions, each displayed with4 digits printed out after the decimal place.
3. If the equation has solutions that are not real numbers, display COMPLEX SOLUTIONS:, followed by two solutions displayed in the forma+bi, where each a and b should be displayed with 4 digits printed out after the decimal place
.For example, a run might look like
Enter x^2 coefficient: 1
Enter x^1 coefficient: -2
Enter x^0 coefficient: 1
ONE SOLUTION: x = 1.0000
or
Enter x^2 coefficient: 3
Enter x^1 coefficient: 5
Enter x^0 coefficient: 1
TWO REAL SOLUTIONS: x = -0.2324 and x = -1.4343
or
Enter x^2 coefficient: 2.1
Enter x^1 coefficient: 4
Enter x^0 coefficient: 10
COMPLEX SOLUTIONS: x = -0.9524 - 1.9634i and x = -0.9524 + 1.9634i
In the last case, note that the letter that is printed out to represent the square root of−1 is the letter i,not the letter j!Python has some functions that produce complex values, but when you print these values, they will display the letter j to represent√−1. I don’t want that – so, instead, your program should calculate the imaginary coefficient, and then include code which prints out the string"i".Do not use the cmath module, the complex function, the .imag attribute,or any Python functions that perform replacements in strings.(If you don’t know what any of those are, don’t worry about it.)
Specifications: your program must
•ask for and accept coefficients from the user.
•assuming that the user enters a non-zero leading coefficient, display the type of solutions as above. (In the case where there is exactly one solution, it is acceptable if the program occasionally misidentifies it, although it should work with the example I’ve provided.)
•display all solutions with exactly 4 digits printed after each decimal (for complex solutions, 4 digits after the decimal for both real and imaginary parts).
•use the letter i to represent √−1 in output, not the letter j.
•not use the cmath module, the complex function, the .imag attribute, or any Python functions that perform replacements in strings.
Challenge: write the program so that it gives the full, correct solution set for x for any real numbers a, b, c that are input,even if the leading coefficient is 0. Warning: I will be extremely stingy about awarding full credit, so make sure it behaves correctly for every last case! (Aside from the caveat I mentioned in the second specification above.)
a=float(input('Enter x^2 coefficient: ')) b=float(input('Enter x^1 coefficient: ')) c=float(input('Enter x^0 coefficient: ')) if a==0: print('ONE SOLUTION: x = {:.4f}'.format(-c/b)) else: D=b*b-4*a*c if abs(D)<10**(-6): print('ONE SOLUTION: x = {:.4f}'.format(-b/(2*a))) elif D>0: print('TWO REAL SOLUTIONS: x = {:.4f} and x = {:.4f}'.format((-b+D**.5)/(2*a),(-b-D**.5)/(2*a))) else: print('COMPLEX SOLUTIONS: x = {:.4f} - {:.4f}i and x = {:.4f} + {:.4f}i'.format(-b/(2*a),(-D)**.5/(2*a),-b/(2*a),(-D)**.5/(2*a)))
Get Answers For Free
Most questions answered within 1 hours.