mediumUnit 2 · Data Types: int, float, complex
P6. Quadratic Equation Roots
Data Types: int, float, complex
Read the coefficients a, b and c of the equation ax² + bx + c = 0 and find the discriminant d = b² − 4ac.
| Discriminant | Nature of roots |
|---|---|
| d > 0 | Real and distinct |
| d = 0 | Real and equal |
| d < 0 | Complex |
Find the roots with (−b ± √d) / 2a, taking + for Root 1 and − for Root 2. Use math.sqrt() when d ≥ 0, and cmath.sqrt() when d < 0 (the roots are then complex numbers).
Input Format
- Line 1:
a - Line 2:
b - Line 3:
c
Output Format
- Line 1:
Nature: <nature> - Lines 2 and 3:
Root 1: <r1>andRoot 2: <r2>. For real roots, print them rounded to 2 decimal places. For complex roots, print them as<real>+<imag>jor<real>-<imag>j, with both parts rounded to 2 decimal places (for example-1.00+2.00j)
Constraints
- 1 ≤
a≤ 100 - -1000 ≤
b,c≤ 1000
Sample Test Cases
Sample 1
Input
1 -3 2
Output
Nature: Real and distinct Root 1: 2.00 Root 2: 1.00
Sample 2
Input
1 2 5
Output
Nature: Complex Root 1: -1.00+2.00j Root 2: -1.00-2.00j
Submitting checks 2 sample cases and 5 hidden test cases.