In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of October 18th
(this exercise will still be available for submission after that deadline, but without couting towards your grade)
[to understand the context of this problem, you should read the class #02 exercise sheet]
Quadratic equations play a fundamental role in algebra and mathematics. In this exercise, you will write a program that calculates the roots of a quadratic equation given its coefficients.
Write a program that calculates the roots of a quadratic equation of the form:
\[
ax^2 + bx + c = 0
\]
Remember that the roots can be real or complex. Use math.sqrt
in your calculations.
The input consists of three floating-point numbers:
The output should display the roots of the equation in the following formats:
"R = [value]"
R1 = [value]"
and "R2 = [value]"
"R1 = [real_part] + [imaginary_part]i"
and "R2 = [real_part] - [imaginary_part]i"
All the printed numbers should be rounded to two decimal places.
Note that when the default behavior of Python's print is to ignore zeros on the far right of the decimal part (e.g. 37.1000 is printed as 37.1 and 2.00 is printed as 2.0). This is ok and you should just print the answer rounded to two decimal places and let Python print the obtained number as in the examples).
The following limits are guaranteed in all the test cases that will be given to your program:
a ≠ 0 | The coefficient a will not be zero | |
-100 <= a, b, c <= 100 | Range for coefficients |
Example Input 1 | Example Output 1 |
1 -5 6 |
R1 = 3.0 R2 = 2.0 |
Example Input 2 | Example Output 2 |
1 4 5 |
R1 = -2.0 + 1.0i R2 = -2.0 - 1.0i |
Example Input 3 | Example Output 3 |
1 -4 4 |
R = 2.0 |