Write a program that prompts the user to input the three coefficients a, b, and c of a quadratic equation ar+ br + c= 0. The program should display the solut ions of this equation, in the following manner: 1. If the equation has one solution, display ONE SOLUTION:, followed by the solution, displayed with 4 digits printed out after the decimal place. 2. If the equation has two real solutions, display TWO REAL SOLUTIONS:, followed by the two solut ions, each displayed with 4 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 form a + bi, where each a and b should be displayed with 4 digits printed out after the decimal place.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter7: User-defined Simple Data Types, Namespaces, And The String Type
Section: Chapter Questions
Problem 7PE
icon
Related questions
Question
Simplest terms python
1) quadratic.Py
Write a program that prompts the user to input the three coefficients a, b, and c of a quadratic equation ar+ br + c = 0.
The program should display the solut ions of this equation, in the following manner:
1. If the equation has one solution, display ONE SOLUTION:, followed by the solut ion, displayed with 4 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 with
4 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 form a + 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
Ent er x1 coefficient: -2
Enter x0 coefficient: 1
ONE SOLUTION: x = 1.0000
or
Ent er x*2 coefficient: 3
Enter x1 coefficient: 5
Enter x"0 coefficient: 1
TWO REAL SOLUTIONS: I = -0.2324 and x = -1.4343
or
Ent er x 2 coefficient: 2.1
Ent er 1*1 coefficient : 4
Enter x"0 coefficient: 10
COMPLEX SOLUTIONS: x = -0.9524 - 1.9634i and I = -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 funct ions that produce complex values, but when you print these values, they will display the letter j to
represent v-1. I don't want that
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 whyat any of those are. don't worry
about, it.)
so, instead, your program should calculate the imaginary coefficient, and then include
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 solut ions as above. (In the case w here
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 aft er each decimal (for complex solutions. 4 digits after the decimal for
both real and imaginary parts).
• use the letter i to represent V-1 in output. not the letter j.
• not use the cmath module, the complex funct ion, the.imag attribute. or any Python functions that perform
replacements in strings.
Transcribed Image Text:1) quadratic.Py Write a program that prompts the user to input the three coefficients a, b, and c of a quadratic equation ar+ br + c = 0. The program should display the solut ions of this equation, in the following manner: 1. If the equation has one solution, display ONE SOLUTION:, followed by the solut ion, displayed with 4 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 with 4 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 form a + 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 Ent er x1 coefficient: -2 Enter x0 coefficient: 1 ONE SOLUTION: x = 1.0000 or Ent er x*2 coefficient: 3 Enter x1 coefficient: 5 Enter x"0 coefficient: 1 TWO REAL SOLUTIONS: I = -0.2324 and x = -1.4343 or Ent er x 2 coefficient: 2.1 Ent er 1*1 coefficient : 4 Enter x"0 coefficient: 10 COMPLEX SOLUTIONS: x = -0.9524 - 1.9634i and I = -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 funct ions that produce complex values, but when you print these values, they will display the letter j to represent v-1. I don't want that 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 whyat any of those are. don't worry about, it.) so, instead, your program should calculate the imaginary coefficient, and then include 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 solut ions as above. (In the case w here 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 aft er each decimal (for complex solutions. 4 digits after the decimal for both real and imaginary parts). • use the letter i to represent V-1 in output. not the letter j. • not use the cmath module, the complex funct ion, the.imag attribute. or any Python functions that perform replacements in strings.
Expert Solution
Step 1

Sample Response:

#Python program to find the roots of quadratic equation
import math
# function to finding roots 
def equationroots(a,b,c):
    #compute discriminant 
    dis=b*b-4*a*c  
    sqrt_val=math.sqrt(abs(dis))  
    #check the Discriminant and generates the solution
    if dis>0:  
        print("TWO REAL SOLUTIONS: x = {0:.4f}".format((-b+sqrt_val)/(2*a))," and x = {0:.4f}".format((-b -sqrt_val)/(2*a)))
    elif dis==0:  
        print("ONE SOLUTION: x = {0:.4f}".format(-b/(2*a)))
    else: 
        print("COMPLEX ROOTS: x = {0:.4f}".format((-b/(2*a))),"+ {0:.4f}".format(sqrt_val),"i and x = {0:.4f}".format((-b/(2*a))),"- {0:.4f}".format(sqrt_val),"i")
#Driver Program
a=float(input("Enter x^2 coefficient: "))
b=float(input("Enter x^1 coefficient: "))
c=float(input("Enter x^0 coefficient: "))
# If a is 0, then incorrect equation 
if a==0:
        print("Input correct quadratic equation")
else: 
    equationroots(a, b, c)

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Introduction to computer system
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT